【问题标题】:binding to a Set in Spring在 Spring 中绑定到 Set
【发布时间】:2009-07-27 06:26:48
【问题描述】:

如果我有一个列表对象,我知道我可以使用下面的代码将类属性字段绑定到表单。

<c:forEach items="${items}" var="i" varStatus="itemsRow">
   <input name="items[${itemsRow.index}].fieldName" type="text"/>
</c:forEach> <form:errors path="items" />

如果属性是 Set 对象,我该怎么办。我已经在 Controller 类中阅读了有关 initBinder 的信息,但它不起作用。任何人都可以在此处粘贴代码来完成此操作吗?

【问题讨论】:

    标签: java spring jsp binding set


    【解决方案1】:

    你需要使用复选框控件:

    <form:form>
          <table>
              <tr>
                  <td>Interests:</td>
                  <td>
                      <%-- Approach 2: Property is of an array or of type java.util.Collection --%>
                      Quidditch: <form:checkbox path="preferences.interests" value="Quidditch"/>
                      Herbology: <form:checkbox path="preferences.interests" value="Herbology"/>
                      Defence Against the Dark Arts: <form:checkbox path="preferences.interests"
                          value="Defence Against the Dark Arts"/>
                  </td>
              </tr>
          </table>
      </form:form>
    

    documentation查看更多信息

    【讨论】:

    • Set 对象是 Set 并且需要显示文本字段以便可以修改图像名称等...上述方法适用于不输入文本字段的复选框,有人可以粘贴一些代码关于如何使用initbinder。我认为这个问题可以使用Controller类中的initBinder方法来解决
    • 我也面临同样的问题。您找到解决方案了吗。谢谢
    猜你喜欢
    • 1970-01-01
    • 2012-10-31
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多