将命令对象从 SimpleFormController 传递到控制器
我有一个要求,我需要将 Spring MVC 中的命令对象从 SimpleFormController 传递到控制器(它是实现控制器接口的控制器)。 Spring的版本是2.5,配置是基于XML的。
从 SimpleFormController 重定向到控制器。但是,即使我在控制器指向的 jsp 中添加了 session="true",对 ${command.myVal} 的调用也会返回 null。
SimpleFormController 中的重定向代码是:
return new ModelAndView("redirect:/SimpleController.do");
对于 SimpleFormController,sessionForm 属性在 spring.xml 中设置为 true
<property name="sessionForm"><value>true</value></property>
为了解决这个问题,我必须将 SimpleFormController 中的命令对象显式添加到 HttpSession 中:
HttpSession session = request.getSession(true);
session.setAttribute("command", commandObj);
在 Simplecontroller 中,我必须创建字符串到 Obj 的映射并将其传递给视图。在这里,我再次从会话中获取了 commandObj。
HttpSession session = request.getSession(true);
resultMap.put("command", session.getAttribute("command"));
请建议是否有更好的方法而不使用注释。这在某种程度上似乎是一种冗长的获得结果的方法。
I have a requirement where I need to pass a command object in Spring MVC from SimpleFormController to a Controller (Its a controller that implements the Controller Interface). The version of Spring is 2.5 and the configuration is XML based.
From the SimpleFormController a redirect is being made to the Controller. However, the call to ${command.myVal} returns null even though I have added session="true" in the jsp that the controller points to.
The Redirect Code in SimpleFormController is :
return new ModelAndView("redirect:/SimpleController.do");
For the SimpleFormController, the sessionForm property is set to true in spring.xml
<property name="sessionForm"><value>true</value></property>
To solve this, I had to explicitly add the command object in the SimpleFormController to the HttpSession :
HttpSession session = request.getSession(true);
session.setAttribute("command", commandObj);
And in the Simplecontroller, I had to create a Map of String to Obj and pass that to the View. Here again, I took the commandObj from the Session.
HttpSession session = request.getSession(true);
resultMap.put("command", session.getAttribute("command"));
Please suggest if there is a better way without using Annotations. This somehow seems a long-winded way of getting the result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了让 Spring 解析命令对象,您需要将表单直接提交到扩展 BaseCommandController 的控制器。
http://static .springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html
如果您的 SimpleFormController 本身收到了表单提交,那么您可以将命令存储在会话中,供控制器使用。
(当然,如果您利用 Spring MVC 基于注释的方法,这一切都会变得更容易)
希望这会有所帮助
In order for Spring to parse the command object, you need to submit the form directly to a controller which extends BaseCommandController.
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/BaseCommandController.html
If your SimpleFormController itself received the form submission, then you could store the command in the session, for use by the Controller.
(and ofcourse this would all become easier if you harnessed the annotation-based approach to Spring MVC)
Hope this helps