Java 如何在不使用会话的情况下在 Spring 中将对象从一个控制器传递到另一个控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19586194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:33:16  来源:igfitidea点击:

How to pass the object from one controller to another in Spring without using Session

javaspringsessionspring-mvc

提问by sharad-garg

I have a requirement where the user selects some data from a form and we need to show that selected data on the next page.

我有一个要求,用户从表单中选择一些数据,我们需要在下一页显示所选数据。

At present we are doing this with a session attribute, but the problem with this is that it overwrites the data if the first page is open in another browser tab, where the data is again selected and submitted. So I just want to get rid of this session attribute while transferring data from one controller to another.

目前我们正在使用会话属性执行此操作,但问题在于,如果在另一个浏览器选项卡中打开第一页,再次选择并提交数据,它会覆盖数据。所以我只想在将数据从一个控制器传输到另一个控制器时摆脱这个会话属性。

Note: I am using an XML based Spring configuration, so please show a solution using XML, not annotations.

注意:我使用的是基于 XML 的 Spring 配置,因此请使用 XML 而不是注释来展示解决方案。

回答by Gerard Ribas

I worked with this requirement and I used RedirectAttributes, then you can add this redirect attributes to your model. This is an example:

我处理了这个要求并使用了 RedirectAttributes,然后您可以将此重定向属性添加到您的模型中。这是一个例子:

@RequestMapping(value = "/mypath/{myProperty}", method = RequestMethod.POST)
public String submitMyForm(@PathVariable Long myProperty, RedirectAttributes redirectAttributes) {
    redirectAttributes.addFlashAttribute("message", "My property is: " + myProperty);
    return "redirect:/anotherPage";
}
@RequestMapping(method = RequestMethod.GET)
public String defaultPage(Model model, @RequestParam(required = false) String message) {
    if(StringUtils.isNotBlank(message)) {
        model.addAttribute("message", message);
    }
    return "myPage";
}

Hope it helps.

希望能帮助到你。

回答by Debojit Saikia

Define RedirectAttributesmethod parameter in the the handlermethod that handles form submissionfrom first page:

RedirectAttributes在从第一页handler处理的方法中定义方法参数form submission

@RequestMapping("/sendDataToNextPage", method = RequestMethod.POST)
public String submitForm(
            @ModelAttribute("formBackingObj") @Valid FormBackingObj formBackingObj,
            BindingResult result, 
            RedirectAttributes redirectAttributes) {
    ...
    DataObject data = new DataObject();
    redirectAttributes.addFlashAttribute("dataForNextPage", data);
    ...
    return "redirect:/secondPageURL";
}

The flash attributes are saved temporarily before the redirect (typically in the session) and are available to the request after the redirect and removed immediately.

flash 属性在重定向前临时保存(通常在会话中),重定向后可用于请求并立即删除。

The above redirect will cause the client (browser) to send a request to /secondPageURL. So you need to have a handler method to handle this request, and there you can get access to the DataObject dataset in the submitFormhandler method:

上述重定向将导致客户端(浏览器)向/secondPageURL. 所以你需要有一个处理程序方法来处理这个请求,然后你就可以访问处理程序方法中的DataObject data集合submitForm

@RequestMapping(value = "/secondPageURL", method = RequestMethod.GET)
public String gotoCountrySavePage(
            @ModelAttribute("dataForNextPage") DataObject data,
            ModelMap model) {
    ...
    //data is the DataObject that was set to redirectAttributes in submitForm method
    return "pageToBeShown";
}

Here DataObject datais the object that contains data from the submitFormmethod.

DataObject data是包含来自submitForm方法的数据的对象。

回答by Yasir Shabbir Choudhary

You can use RedirectAttributes ; A specialization of the Model interface that controllers can use to select attributes for a redirect scenario.

您可以使用 RedirectAttributes ;控制器可以用来为重定向场景选择属性的模型接口的特殊化。

public interface RedirectAttributes extends org.springframework.ui.Model

Plus this interface also provide a way to store "Flash Attribute" . Flash Attribute is in FlashMap.

另外这个接口还提供了一种存储“Flash Attribute”的方法。Flash 属性在 FlashMap 中。

FlashMap: A FlashMap provides a way for one request to store attributes intended for use in another. This is most commonly needed when redirecting from one URL to another. Quick Example is

FlashMap:FlashMap 为一个请求提供了一种存储用于另一个请求的属性的方法。当从一个 URL 重定向到另一个 URL 时,这是最常用的。快速示例是

@RequestMapping(value = "/accounts", method = RequestMethod.POST)
public String handle(RedirectAttributes redirectAttrs) {
 // Save account ...
 redirectAttrs.addFlashAttribute("message", "Hello World");
 return "redirect:/testUrl/{id}";
 }

Reference and detail information are here

参考和详细信息在这里