Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send data to a redirected URL spring mvc

I have an URL which redirects to another URL when called. I would like to pass some data along with the redirection.

For example I have this method:

@RequestMapping("efetuaEnvioEmail")
    public String efetuaEnvioEmail(HttpServletResponse response) throws IOException {
        System.out.println("efetuaEnvioEmail");
        return "redirect:inicio";
    }

Which redirects to this one:

@RequestMapping("inicio")
    public String Inicio(HttpServletRequest request) throws IOException {

        return "Inicio";
    }

I would like to pass some data informing that everything went fine on the first method.

I have tried some methods of HttpServletRequest and HttpServletResponse but I couldn't have anything.

like image 515
Felipe Mosso Avatar asked Jun 24 '13 19:06

Felipe Mosso


1 Answers

Use RedirectAttributes to pass any data between handler methods:

@RequestMapping("efetuaEnvioEmail")
public String efetuaEnvioEmail(RedirectAttributes rattrs) {
    rattrs.addAttribute("string", "this will be converted into string, if not already");
    rattrs.addFlashAttribute("pojo", "this can be POJO as it will be stored on session during the redirect");
    return "redirect:inicio";
}

@RequestMapping("inicio")
public String Inicio(@ModelAttribute("pojo") String pojo) {
    System.out.println(pojo);
    return "Inicio";
}
like image 178
Pavel Horal Avatar answered Sep 24 '22 17:09

Pavel Horal