【发布时间】:2014-05-08 08:23:55
【问题描述】:
问题是我的控制器中有一个弹簧表单和 2 个具有相同属性的 @ModelAttribute 参数。表单的“commandName”参数设置为我的 modelAttributes 名称之一。令我惊讶的是,该属性不仅映射到使用“commandName”指定的模型属性,还映射到第二个。
我在这里没有找到确切的解决方案,除了和我的类似:Spring-form multiple forms with same model atribute name properties
但在我的情况下,我看不到任何“奇怪的东西”,我有一个表单,一个模型属性来绑定这个表单,一个模型属性可以访问控制器作用域的 @SessionAttribute。 我也尝试过使用表单的 'modelAttribute' 参数(实际上我看不出它们之间有什么区别),但没有帮助。
我的代码示例:
view.jsp:
<form:form name="form" action="/myAction" method="POST" commandName="model1">
<form:input path="property"/>
....
<input type="submit" value="Submit"/>
</form:form>
Controller.java
@SessionAttributes("model2")
class Controller {
@RequestMapping(value = "/myAction", method = POST)
public String submitEditSite(final @ModelAttribute(value = "model1") Model1 model1,
final @ModelAttribute(value = "model2") Model2 model2) {
....
return "redirect:/home";
}
}
Model1.java Model2.java
class Model1 {
private String property;
}
class Model2 {
private String property;
}
我哪里错了?
【问题讨论】:
标签: forms spring spring-mvc