简体   繁体   中英

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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM