如何从重定向视图中获取对象?
我目前使用 spring mvc、java 和注释。
@RequestMapping(value = "/submitTask", method = RequestMethod.POST)
public ModelAndView submitTask(HttpSession session, HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
ModelAndView model = new ModelAndView(new RedirectView("home.html"));
map.put("email", request.getParameter("email"));
map.put("task",request.getParameter("task"));
map.put("error", request.getParameter("error"));
model.addObject("map", map);
return model;
}
@RequestMapping("/home")
public ModelAndView home(HttpSession session, HttpServletRequest request) {
ModelAndView model = new ModelAndView("home");
model.addObject("map", request.getParameter("map"));
return model;
}
当我将视图重定向到 home.html 时,我似乎没有通过“request.getParameter(“map”)”获得“map”的值。我怎样才能找回它。谢谢
Im currently using spring mvc, java and annotations.
@RequestMapping(value = "/submitTask", method = RequestMethod.POST)
public ModelAndView submitTask(HttpSession session, HttpServletRequest request) {
Map<String, Object> map = new HashMap<String, Object>();
ModelAndView model = new ModelAndView(new RedirectView("home.html"));
map.put("email", request.getParameter("email"));
map.put("task",request.getParameter("task"));
map.put("error", request.getParameter("error"));
model.addObject("map", map);
return model;
}
@RequestMapping("/home")
public ModelAndView home(HttpSession session, HttpServletRequest request) {
ModelAndView model = new ModelAndView("home");
model.addObject("map", request.getParameter("map"));
return model;
}
I don't seem to get the value of "map" via "request.getParameter("map")" when i redirected my view to home.html. how can i be able to retrieve it. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信问题在于传递给“home”方法的 HttpServletRequest 请求包含参数“map.email”、“map.task”、“map.error”,但不包含“map”。
I belive that the problem is that the HttpServletRequest request passed to "home" method contains the parameter "map.email", "map.task", "map.error", but not "map".
使用
RedirectView
会使浏览器发出新的请求,因此原始请求会丢失。您需要诸如闪光范围或对话范围之类的东西。我不知道这些的任何实现,但是 检查 google 结果。
使用 spring webflow 是一种处理对话的方法,但是对于简单的任务来说它太复杂了。
作为解决方法,您可以使用会话,然后立即清除它(这本质上就是 flash-scope 所做的事情)
Using
RedirectView
makes the browser issue a new request, so the original request is lost.You need something like a flash scope or conversation scope. I don't know of any implementation of these, but check the google results.
Using spring webflow is a way to handle conversations, but it is too complicated for the simple task.
As a workaround you can use the session, and then clear it immediately (which is essentially what the flash-scope would do)
更改
为
重定向将删除服务器端的所有数据,除非会话属性或 bean,因为客户端端将生成新请求。
转发会将所有内容转移到不同的“方法” - 请求属性不会改变,因此它们将是可访问的。
Change
to
Redirect will drop all data on the side of server unless a session attribute or bean as there will be a new request generated from the side of a client.
Forward will transfer everything to a different "method" - request attributes will not be altered, therefore, they will be accessible.