如何使用重定向传递复杂的 ModelAttribute:在 Spring MVC 3.0 中
大家好,我在寻找答案时遇到问题...
用例:
在控制器中读取 Excel 电子表格。处理了 4 行表,第 2 行和第 3 行有一些错误,因此我跳过它们并继续处理其余部分。但我想保留这些行以在处理完成后显示给用户。我想保留行中的所有值以显示回用户只是为了提供足够的信息。
所以我尝试做的是:
@RequestMapping(value = "/bulk-create", method = RequestMethod.POST)
public String bulkCreate(Model model, SpreadSheetFile spreadSheetFile){
...some code...
List<Row> errorRows = new LinkedList<Row>();
...some code to process rows...
//Error Found
errorRows.add(row);
...more code to wrap up processing (possibly more errors)...
model.addAttribute("erroRows", errorRows);
return "redirect:/bulk-review"
}
@RequestMapping(value = "/bulk-review", method = RequestMethod.GET)
public String bulkReview(Model model,
@ModelAttribute("errorRows")
LinkedList<Row> errorRows){
model.addAttribute("errorRows", errorRows);
return "bulk-review";
}
所以基本上我试图将 LinkedList 从一个 MVC 方法传递到另一个 MVC 方法,而不首先实际点击页面(除非我误解了“redirect:”的工作原理......它首先调用控制器方法,正确吗?)
当我但是断点到bulkReview方法中,errorRows为空...那么如何在两个方法之间保留这个值?
感谢您的帮助!如果我需要澄清任何事情,请告诉我! :)
克里斯
Hi all question I'm having problems finding an answer for...
Use Case:
Reading in an Excel spreadsheet in a controller. A 4 row sheet was processed and row 2 and 3 had some errors in it, so I skip them and move on with the rest of the processing. But I want to retain these rows to display to the user after the processing is complete. I'd like to retain all the values in the row to display back to the user just to provide enough information.
So what I tried to do was this:
@RequestMapping(value = "/bulk-create", method = RequestMethod.POST)
public String bulkCreate(Model model, SpreadSheetFile spreadSheetFile){
...some code...
List<Row> errorRows = new LinkedList<Row>();
...some code to process rows...
//Error Found
errorRows.add(row);
...more code to wrap up processing (possibly more errors)...
model.addAttribute("erroRows", errorRows);
return "redirect:/bulk-review"
}
@RequestMapping(value = "/bulk-review", method = RequestMethod.GET)
public String bulkReview(Model model,
@ModelAttribute("errorRows")
LinkedList<Row> errorRows){
model.addAttribute("errorRows", errorRows);
return "bulk-review";
}
So basically I'm trying to pass a LinkedList from one MVC method to another without actually hitting a page first (unless I'm misunderstanding how "redirect:" works... it calls the controller method first correct?)
When I but a break point into the bulkReview method, errorRows is empty... So how do I retain this value between the two methods?
Thanks for the help! Let me know if I need to clarify anything! :)
Kris
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为重定向机制并不像您想象的那样工作。第一个控制器将 URL 发送回用户的浏览器,然后浏览器会跟踪该 URL。默认情况下,Spring 在重定向时将模型中的所有数据放入重定向 URL 中,以便重定向到的页面可以“看到”模型数据。但是,当模型中有复杂数据时,这种方法效果不佳。
因此,您需要考虑如何在浏览器发送回用户的重定向 URL 中传递数据。 List 如何序列化到 URL?可能不太好。
另一种选择是将数据从一个控制器放入会话中并在第二个控制器中访问它。
I don't think the redirect mechanism works the way you think it does. The first controller sends back a URL to the user's browser which the browser then follows. And by default, Spring places any data in the model at the time of a redirect in the redirect URL, so that the page-being-redirected to can "see" the model data. However, this doesn't work well when you have complex data in the model.
So, you need to think about how the data will be passed in the redirect URL that the browser sends back to the user. How does a List get serialized to the URL? probably not very well.
Another option would be to put the data in session from one controller and access it in the second.