微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 如何在spring-mvc中将参数传递给重定向页面

我写过以下控制器:

@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";
    }

如何在home.jsp页面上更改此代码我可以编写${message}并查看“成功注销”

最佳答案
当返回值包含redirect:前缀时,viewResolver会将此识别为需要重定向的特殊指示.视图名称的其余部分将被视为重定向URL.客户端将向此重定向URL发送新请求.因此,您需要将映射到此URL的处理程序方法处理重定向请求.

您可以编写这样的处理程序方法来处理重定向请求:

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

您可以重写logout处理程序方法,如下所示:

@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";
}

编辑:

您可以在应用程序配置文件中使用此条目来避免showHomePage方法

这会将/ home的请求转发给名为home的视图.如果在视图生成响应之前没有要执行的Java控制器逻辑,则此方法是合适的.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐