Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to redirect page in spring-mvc

I have wrote following controller:

@RequestMapping(value="/logOut", method = RequestMethod.GET )
    public String logOut(Model model, RedirectAttributes redirectAttributes)  {
        redirectAttributes.addFlashAttribute("message", "success logout");
        System.out.println("/logOut");
        return "redirect:home.jsp";
    }

How to change this code that on page home.jsp I can to write ${message} and to see "success logout"

like image 667
gstackoverflow Avatar asked Oct 08 '13 13:10

gstackoverflow


1 Answers

When the return value contains redirect: prefix, the viewResolver recognizes this as a special indication that a redirect is needed. The rest of the view name will be treated as the redirect URL. And the client will send a new request to this redirect URL. So you need to have a handler method mapped to this URL to process the redirect request.

You can write a handler method like this to handle the redirect request:

@RequestMapping(value="/home", method = RequestMethod.GET )
public String showHomePage()  {
    return "home";
}

And you can re-write the logOut handler method as this:

@RequestMapping(value="/logOut", method = RequestMethod.POST )
public String logOut(Model model, RedirectAttributes redirectAttributes)  {
    redirectAttributes.addFlashAttribute("message", "success logout");
    System.out.println("/logOut");
    return "redirect:/home";
}

EDIT:

You can avoid showHomePage method with this entry in your application config file:

<beans xmlns:mvc="http://www.springframework.org/schema/mvc"
 .....
 xsi:schemaLocation="...
 http://www.springframework.org/schema/mvc
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 ....>

<mvc:view-controller path="/home" view-name="home" />
 ....
</beans>

This will forward a request for /home to a view called home. This approach is suitable if there is no Java controller logic to execute before the view generates the response.

like image 61
Debojit Saikia Avatar answered Sep 21 '22 03:09

Debojit Saikia