在 Spring MVC 3 中处理后重定向时,会话属性是否显示在 URL 中?
我正在 Spring MVC 中处理表单,并且提交了一个表单,我正在尝试重定向发布请求以避免使用刷新按钮重新提交表单。
但我需要根据重定向页面中的表单提交来显示动态生成的值。因此,我将该值保存在帖子处理程序方法的会话中,并在重定向页面处理方法中将其取回。
我是否可以获得重定向页面中显示的会话属性,如 GET 方法情况下的请求参数?
以下是我正在使用的代码:
此方法正在处理表单提交
@RequestMapping(value="/something", method=RequestMethod.POST)
public String testStopped(Model model, WebRequest request, HttpSession session) {
//...
int foo = 1234;//some dynamically generated value
session.setAttribute("foo", foo);
return "redirect:/something/somethingelse";
}
此方法正在处理重定向页面
@RequestMapping(value="/something/somethingelse")
public String testStopped(Model model, HttpSession session) {
...
Integer kungfoo = (Integer) session.getAttribute("foo");
model.addAttribute("kungfoo", kungfoo);
return "somethingelse";
}
这是重定向后我最终得到的网址: http://wikedlynotsmart.com/something/somethingelse?kungfoo=1234
有没有办法让 ?kungfoo=1234
不显示在最后并且仍然将其传递给重定向请求处理程序方法吗?
这是它应该如何工作的,还是我在某个地方犯了错误?有人可以帮我理解吗?
谢谢。
I am handling forms in Spring MVC and I submitted a form and I am trying to redirect the post request to avoid resubmitting the form with refresh button.
But I need a dynamically generated value to be displayed based on the form submission in the redirected page. So I am saving that value in session in the post handler method and getting it back in the redirected page handling method.
Would I get the session attribute shown displayed in the redirected page like request parameters in GET method case?
Following is the code that I am using:
This method is handling the form submission
@RequestMapping(value="/something", method=RequestMethod.POST)
public String testStopped(Model model, WebRequest request, HttpSession session) {
//...
int foo = 1234;//some dynamically generated value
session.setAttribute("foo", foo);
return "redirect:/something/somethingelse";
}
This method is handling the redirected page
@RequestMapping(value="/something/somethingelse")
public String testStopped(Model model, HttpSession session) {
...
Integer kungfoo = (Integer) session.getAttribute("foo");
model.addAttribute("kungfoo", kungfoo);
return "somethingelse";
}
This is the url I end up with after redirect: http://wikedlynotsmart.com/something/somethingelse?kungfoo=1234
Is there a way to for ?kungfoo=1234
not to be displayed at the end and still get it passed to the redirect request handler method?
Is this how it is supposed to work or I am committing a blunder somewhere? Could someone help me understand it?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
引用自 文档:
这意味着 kungfoo 是第一个方法中模型的一个属性,因此它会作为请求参数自动添加到重定向 URL。
本段的其余部分解释了如果您不需要重定向 URL 中的所有属性,则如何继续(尽管我感觉默认值正是您应该使用的,在这种情况下,因为您希望第二个请求获得返回 kungfoo 值):
如果您想继续使用在会话中存储值的解决方案,则应使用 flash 属性,以便在收到第二个请求后立即将其从会话中删除。本段的其余部分解释了如何使用它们。
Quote from the documentation:
This means that kungfoo is an attribute of your model in the first method, and that it's thus automatically added as a request param to the redirect URL.
The rest of the paragraph explains how to proceed if you don't want all the attributes in the redirect URL (although I have the feeling that the default is exactly what you should use, in this case, since you want the second request to get back the kungfoo value):
If you want to stay with this solution of storing the value in the session, you should use a flash attribute, so it's removed from the session as soon as the second request is received. The rest of the paragraph explains how to use them.
以下是对我有用的方法:
添加
RedirectAttributes
类型的参数并将foo
添加为 flash 属性。该方法正在处理表单提交
该方法正在处理重定向页面;对于“foo”,这里不需要做任何事情
这就是在somethingelse.jsp中使用“foo”的方式
和 http://wikedlynotsmart.com/somethingelse 是我在网址中得到的内容。
Here is what worked for me:
Adding an argument of type
RedirectAttributes
and addingfoo
as a flash attribute to it.This method is handling the form submission
This method is handling the redirected page; nothing needs to be done here for "foo"
This is how you use "foo" in somethingelse.jsp
And http://wikedlynotsmart.com/somethingelse is what I get in the URL.