我有一个要求,我需要将Spring MVC中的命令对象从SimpleFormController传递给Controller(它是一个实现Controller接口的控制器)。 Spring的版本是2.5,配置是基于XML的。
从SimpleFormController中,正在向Controller进行重定向。但是,对$ {command.myVal}的调用返回null,即使我在控制器指向的jsp中添加了session =“true”。
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创建一个String of Map并将其传递给View。在这里,我从Session中获取了commandObj。
HttpSession session = request.getSession(true);
resultMap.put("command", session.getAttribute("command"));
如果没有使用注释,请建议是否有更好的方法。这在某种程度上似乎是一种啰嗦的方式来获得结果。
答案 0 :(得分:0)
为了让Spring解析命令对象,您需要将表单直接提交给扩展BaseCommandController的控制器。
如果您的SimpleFormController本身收到表单提交,那么您可以将该命令存储在会话中,供Controller使用。
(当然,如果你利用基于注释的Spring MVC方法,这将变得更容易)
希望这有帮助