使用Spring的form标签动态绑定列表 [英] Dynamically binding lists with Spring's form tag

查看:120
本文介绍了使用Spring的form标签动态绑定列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个命令对象FaxForm,它在faxStatusList属性内保存了一个FaxStatus对象的列表.

I have a command object FaxForm and it holds a list of FaxStatus objects inside a faxStatusList property.

public class FaxForm {
  private List<FaxStatus> faxStatusList;
  public void setFaxStatusList(List<FaxStatus> faxStatusList) {
    this.faxStatusList = faxStatusList;
  }
  public List<FaxStatus> getFaxStatusList() {
    return faxStatusList;
  }
}

最初,我有一个JSP页面,该页面将通过执行以下操作来绑定对象:

I initially had a JSP page that would bind the objects by performing the following:

<c:forEach items="${esaFaxForm.faxStatusList}" var="item" varStatus="loop">
  <tr class="tableAltBackground">
    <td>
      <form:checkbox path="faxStatusList[${loop.index}].selected"/>
    </td>
    <td>
      <form:select path="faxStatusList[${loop.index}].status" items="${esaFaxForm.statusOptions}" onchange="checkThisBox(this);"/>
    </td>
    <td>
      <a href="${statusContUrl}?id=${item.id}&status=${item.status}" onclick="openFaxWindow('${viewFaxUrl}?id=${item.id}', ${loop.index});">${item.name}</a>
      <form:hidden path="faxStatusList[${loop.index}].name"/>
    </td>
    <td>
      <a href="${statusContUrl}?id=${item.id}&status=${item.status}" onclick="openFaxWindow('${viewFaxUrl}?id=${item.id}', ${loop.index});">${item.id}</a>
      <form:hidden path="faxStatusList[${loop.index}].id"/>
    </td>
  </tr>
</c:forEach>

但是,我试图弄清楚如何在没有forEach循环和索引的情况下进行绑定. Spring 网站上的示例通过设置列表名称的路径.有没有办法绑定属性?我已经尝试过了,但是失败了:

However, I am trying to figure out how I could do the binding without the forEach loop and index. The examples on the Spring website show the binding by setting the path to the list name. Is there a way to bind the properties? I've tried this but it fails:

<form:checkbox path="faxStatusList.faxStatus.selected"/>
<form:select path="faxStatusList.faxStatus.status" items="${esaFaxForm.statusOptions}"/>

FaxStatusList具有getter和setter方法,而FaxStatus变量均具有getter/setter属性.我收到错误"Bean类的无效属性'faxStatusList.faxStatus'..."

The faxStatusList has a getter and setter method and the FaxStatus variables each have getter/setter properties. I get the error "Invalid property 'faxStatusList.faxStatus' of bean class..."

推荐答案

Spring表单标签具有复选框标签.您可以按以下方式使用它来自动进行绑定:

Spring form tags has a checkboxes tag. You can use it as follows to do the binding automatically:

<form:checkboxes items="${faxStatusList}" path="faxStatusList" itemLabel="name" itemValue="id" delimiter="<br/>" onclick="yourOnClickMethodIfYouNeed(this);"/>

上面的代码片段将显示一个以br标签分隔的复选框项目列表.对复选框状态所做的任何更改都将适当地反映在您的FaxForm中. FaxStatusList对象.

The above snippet will display a list of checkbox items delimited with the br tag. Any changes made to the state of the checkboxes will be reflected appropriately in your FaxForm. faxStatusList object.

这篇关于使用Spring的form标签动态绑定列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆