How to send an object between controllers via Redirect

Asked

Viewed 580 times

1

How to send an Object through redirect between requests?

I performed tests trying to send the object through the model, but without success.

Follows the code:

 @RequestMapping("removeResultado")
 public String remove(RequestParam(value = "codigo", required = true)int cod,Model model) {
  Evento evento = daoEvento.buscarIDEvento(cod);
  model.addAttribute(evento);
  this.dao.remove(cod);
  return "redirect:listaResultados";
 }


 @RequestMapping("listaResultados")
 public String busca(Evento evento, Model model) {
  List<Resultado> resultados = this.dao.buscaResultadosEvento(evento);
  model.addAttribute("resultados", resultados);
  return  "/Resultado/ListaResultados";
 }

1 answer

2


You can use the Redirectattributes to pass the objects through redirect.

@RequestMapping(value="/suaUrl", method=GET)
public String seuMetodo(RedirectAttributes redirectAttributes)
{
   ...
   redirectAttributes.addAttribute("mensagem", "redirecionamento");
   redirectAttributes.addFlashAttribute("objeto2", objeto2);
   return "redirect:/outraUrl";
}

When using the addAttribute they are used to build a new redirect URL, so the use is limited to primitives and the string.

And when you use the addFlashAttribute data is stored (usually in session) temporarily and is available for request and after redirection is removed. The advantage is that you can add any object.

Original response in English

  • Excellent, addFlashAttribute I think would be the best solution.

  • @Matheus takes the test, if there’s a problem, let me know and I’ll help you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.