如何在spring-mvc中将参数传递给重定向页面 [英] How to pass parameters to redirect page in spring-mvc

查看:159
本文介绍了如何在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} 并查看成功注销

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

推荐答案

当返回值包含重定向:前缀时, viewResolver 将此视为需要重定向的特殊指示。视图名称的其余部分将被视为重定向URL。客户端将向此重定向网址发送新请求。因此,您需要将一个处理程序方法映射到此URL以处理重定向请求。

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

你可以重写 logOut 处理程序方法如下:

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

编辑:

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

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>

这将转发 / home 到一个名为 home 的视图。如果在视图生成响应之前没有要执行的Java控制器逻辑,这种方法是合适的。

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.

这篇关于如何在spring-mvc中将参数传递给重定向页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆