How to send an object between controllers through Redirect

1

How to send an object through redirect between requests?

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

Follow 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";
 }
    
asked by anonymous 29.04.2016 / 14:40

1 answer

2

You can use the RedirectAttributes to pass 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 addAttribute they are used to construct a new redirect URL, so use is limited to primitives and String.

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

Original answer in English

    
29.04.2016 / 15:39