在 Spring MVC 3 中处理后重定向时,会话属性是否显示在 URL 中?

发布于 2024-12-28 11:57:48 字数 1200 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请远离我 2025-01-04 11:57:48

引用自 文档

默认情况下,所有模型属性都被视为公开为 URI
重定向 URL 中的模板变量。其余属性中
那些是原始类型或原始集合/数组
类型会自动附加为查询参数。

这意味着 kungfoo 是第一个方法中模型的一个属性,因此它会作为请求参数自动添加到重定向 URL。

本段的其余部分解释了如果您不需要重定向 URL 中的所有属性,则如何继续(尽管我感觉默认值正是您应该使用的,在这种情况下,因为您希望第二个请求获得返回 kungfoo 值):

在带注释的控制器中,模型可能包含额外的
最初添加用于渲染目的的属性(例如下拉列表
字段值)。为了获得对属性中使用的精确控制
重定向场景,@RequestMapping方法可以声明一个参数
类型 RedirectAttributes 并使用它来添加属性以供使用
重定向视图。如果控制器方法确实重定向,则内容
使用重定向属性。否则默认 Model 的内容
已使用。

如果您想继续使用在会话中存储值的解决方案,则应使用 flash 属性,以便在收到第二个请求后立即将其从会话中删除。本段的其余部分解释了如何使用它们。

Quote from the documentation:

By default all model attributes are considered to be exposed as URI
template variables in the redirect URL. Of the remaining attributes
those that are primitive types or collections/arrays of primitive
types are automatically appended as query parameters.

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):

In annotated controllers however the model may contain additional
attributes originally added for rendering purposes (e.g. drop-down
field values). To gain precise control over the attributes used in a
redirect scenario, an @RequestMapping method can declare an argument
of type RedirectAttributes and use it to add attributes for use in
RedirectView. If the controller method does redirect, the content of
RedirectAttributes is used. Otherwise the content of the default Model
is used.

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.

水波映月 2025-01-04 11:57:48

以下是对我有用的方法:

添加 RedirectAttributes 类型的参数并将 foo 添加为 flash 属性。

该方法正在处理表单提交

@RequestMapping(value="/something", method=RequestMethod.POST) 
public String testStopped(Model model, WebRequest request, RedirectAttributes redirectAttributes) {
    //...
    int foo = 1234;
    redirectAttributes.addFlashAttribute("foo", foo);
    return "redirect:/something/somethingelse";
}

该方法正在处理重定向页面;对于“foo”,这里不需要做任何事情

@RequestMapping(value="/something/somethingelse") 
public String testStopped() {
    return "somethingelse";
}

这就是在somethingelse.jsp中使用“foo”的方式

${foo}

http://wikedlynotsmart.com/somethingelse 是我在网址中得到的内容。

Here is what worked for me:

Adding an argument of type RedirectAttributes and adding foo as a flash attribute to it.

This method is handling the form submission

@RequestMapping(value="/something", method=RequestMethod.POST) 
public String testStopped(Model model, WebRequest request, RedirectAttributes redirectAttributes) {
    //...
    int foo = 1234;
    redirectAttributes.addFlashAttribute("foo", foo);
    return "redirect:/something/somethingelse";
}

This method is handling the redirected page; nothing needs to be done here for "foo"

@RequestMapping(value="/something/somethingelse") 
public String testStopped() {
    return "somethingelse";
}

This is how you use "foo" in somethingelse.jsp

${foo}

And http://wikedlynotsmart.com/somethingelse is what I get in the URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文