SpringMVC form:options items 属性:它到底期望什么?

2024-05-12

我对 SpringMVC (以及 jstl 来说)还是个新手。我正在尝试填充从对象列表中选择的选项。我找到了一种使用 c:forEach 来做到这一点的方法,但我一直认为必须有一种方法可以使 form:options 方法起作用。

我浏览了一下,我能找到的关于 items 属性的官方文档最接近的内容在这里 >>http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html#spring-form.tld.options http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html#spring-form.tld.options

它说 items 属性用于

“用于生成内部‘选项’标签的对象的集合、映射或数组”

我的困惑是它正在寻找什么样的集合、映射或对象数组。它们需要采用什么格式?它是专门寻找 String 类型的 Collection 或数组吗?我可以用吗

List<MyObject>

如果是这样,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;
    }
}

我正在尝试这样使用它:

<form:select path="myObjectList">
    <form:option value="0"/>
    <form:options items="myObjectList" /> 
</form:select>

有谁知道这个方法有什么不正确的地方吗?或者,我应该使用

List<String> 

完成我正在做的事情?

EDIT这是堆栈跟踪>>http://pastebin.com/2c5XBCmG http://pastebin.com/2c5XBCmG


The Spring文档 http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/view.html说的是关于items的属性form:options tag:

items 属性通常由集合或数组填充 项目对象。 itemValue 和 itemLabel 只是引用 bean 这些项目对象的属性(如果指定);否则,该项目 对象本身将被字符串化。或者,您可以指定 项目映射,在这种情况下映射键被解释为选项 值和地图值对应于选项标签。如果项目值 和/或 itemLabel 也恰好被指定,项目值 属性将应用于地图键,项目标签属性将应用于 适用于地图值。

简而言之,如果您需要使用自定义 Bean 列表作为 items 属性,您还需要使用itemValue and itemLabel属性。就我个人而言,我更喜欢使用地图 -LinkedHashMap具体来说 - 用于填充我选择的标签,但这是一个品味问题。

根据 Spring 文档中的示例,您的代码应如下所示:

 <form:select path="commandAttribute">
      <form:option value="-" label="--Please Select"/>
      <form:options items="${countryList}" itemValue="company" itemLabel="company"/>
 </form:select>

我正在使用company属性同时为itemValue and itemLabel,但您可以自由选择适合您要求的属性。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringMVC form:options items 属性:它到底期望什么? 的相关文章

随机推荐