我还是SpringMVC的新手(和jstl一样).我正在尝试从对象列表中选择一个选项.我已经找到了一种方法来使用c:forEach,但我一直认为有一种方法可以使表单:选项方法工作.
我浏览了一下,关于items属性的官方文档中我能找到的最接近的东西是>> http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld的.html#弹簧form.tld.options
它表示items属性适用于
"用于生成内部'选项'标签的集合,地图或对象数组"
我的困惑是它正在寻找什么样的Collection,Map或对象数组.他们需要采用什么格式?它是否专门寻找String类型的Collection或数组?我可以用吗
List<MyObject>
Run Code Online (Sandbox Code Playgroud)
如果是这样,MyObject必须拥有什么才能使它有效(即方法,变量)?
目前,当我尝试使用MyObject时,我得到一个例外,说 -
ConverterNotFoundException:找不到能够从类型com.example.MyObject转换为java.lang.String类型的转换器
我需要制作转换器吗?那会怎么样?那会怎么样?我用谷歌搜索了这个错误信息,并没有真正发现任何特定于我正在尝试做的事情...(大多数是关于Roo的结果)
MyObject类如下所示:
public class MyObject{
private String company;
private Customer customer;
private Address customerAddress;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public Address getCustomerAddress() {
return customerAddress;
}
public void setCustomerAddress(Address customerAddress) {
this.customerAddress = customerAddress;
}
}
Run Code Online (Sandbox Code Playgroud)
而我正试图这样使用它:
<form:select path="myObjectList">
<form:option value="0"/>
<form:options items="myObjectList" />
</form:select>
Run Code Online (Sandbox Code Playgroud)
有谁知道这个方法有什么不正确之处?或者,我应该使用
List<String>
Run Code Online (Sandbox Code Playgroud)
完成我正在做的事情?
编辑这里的堆栈跟踪>> http://pastebin.com/2c5XBCmG
Car*_*ron 22
在Spring文档说这有关items的属性form:options标签:
items属性通常使用项目对象的集合或数组填充.如果指定,itemValue和itemLabel只是引用那些项目对象的bean属性; 否则,项目对象本身将被字符串化.或者,您可以指定项目的地图,在这种情况下,地图关键字被解释为选项值,地图值对应于选项标签.如果恰好也指定了itemValue和/或itemLabel,则item value属性将应用于map键,item label属性将应用于map值.
简而言之,如果您需要使用自定义Bean列表作为items属性,则还需要使用itemValue和itemLabel属性.就个人而言,我更喜欢使用地图 - LinkedHashMap特定的实例- 来填充我的选择标签,但这是一个品味问题.
调整Spring文档中的示例,您的代码应如下所示:
<form:select path="commandAttribute">
<form:option value="-" label="--Please Select"/>
<form:options items="${countryList}" itemValue="company" itemLabel="company"/>
</form:select>
Run Code Online (Sandbox Code Playgroud)
我使用的company属性,既itemValue和itemLabel,但你可以自由地选择适合您的需求的属性.
| 归档时间: |
|
| 查看次数: |
49585 次 |
| 最近记录: |