How to pass parameters to redirect page in spring-mvc

后端 未结 1 1756
生来不讨喜
生来不讨喜 2021-01-02 15:18

I have wrote following controller:

@RequestMapping(value=\"/logOut\", method = RequestMethod.GET )
    public String logOut(Model model, RedirectAttributes r         


        
相关标签:
1条回答
  • 2021-01-02 16:16

    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.

    0 讨论(0)
提交回复
热议问题