【问题标题】:Spring form path with multiple model attributes with the same property name具有多个具有相同属性名称的模型属性的 Spring 表单路径
【发布时间】: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


    【解决方案1】:

    如果我理解正确,您想阻止在model2 上设置任何属性,对吗? 那么应该这样做:

      @InitBinder("model2")
      public void initBinder(WebDataBinder binder) {
        binder.setDisallowedFields("*");
      }
    

    【讨论】:

      猜你喜欢
      • 2015-04-09
      • 2014-08-18
      • 1970-01-01
      • 2018-08-05
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多