SpringMVC form:options items属性:究竟是什么期望? [英] SpringMVC form:options items attribute: what exactly is it expecting?

查看:149
本文介绍了SpringMVC form:options items属性:究竟是什么期望?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还是SpringMVC的新手(和jstl一样).我试图填充对象列表中的选择中的选项.我已经找到了一种使用c:forEach的方法,但是我一直在想有一种方法可以使form:options方法起作用.

I'm still new to SpringMVC (and jstl for that matter). I'm trying to populate options in a select from a list of objects. I've found a way to do it using c:forEach, but I keep thinking there HAS to be a way to make the form:options method work.

我已经浏览了一下,关于物品属性我可以找到的与官方文档最接近的地方是这里>>

I've browsed around, and about the closest thing I can find to official documentation on the items attribute is here >> http://static.springsource.org/spring/docs/2.0.x/reference/spring-form.tld.html#spring-form.tld.options

它表示items属性用于

It says the items attribute is for

用于生成内部'option'标签的对象的Collection,Map或数组"

"The Collection, Map or array of objects used to generate the inner 'option' tags"

我的困惑是正在寻找什么样的集合,地图或对象数组.他们需要采用什么格式?它是专门寻找String类型的Collection还是数组?我可以使用

My confusion is what kind of Collection, Map, or array of objects it's looking for. What format do they need to be in? Is it looking for a Collection or array of type String specifically? Can I use

List<MyObject>

,如果是这样,MyObject必须具有什么才能使其有效(即方法,变量)?

and if so, what would MyObject have to have in it in order for this to be valid (i.e. methods, variables)?

当前,当我尝试使用MyObject时,出现一个异常-

Currently, when I try to use MyObject, I get an exception that says -

ConverterNotFoundException:未找到能够从com.example.MyObject类型转换为java.lang.String类型的转换器

ConverterNotFoundException: No converter found capable of converting from type com.example.MyObject to type java.lang.String

我需要制造转换器吗?那会去哪里?那将如何工作?我已经搜索了该错误消息,但还没有真正找到我要尝试执行的操作...(大多数是有关Roo的结果)

Do I need to make a converter? Where would that go? How would that work? I've googled that error message and haven't really turned up anything specific to what I'm trying to do... (Most are results about Roo)

MyObject类如下:

the MyObject class looks like this:

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;
    }
}

而我正试图将其用于此类目的:

and I'm trying to use it as such:

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

有人知道这种方法有什么不正确吗?或者,我应该使用

Does anyone know specifically what is incorrect about this method? Or, should I be using a

List<String> 

完成我在做什么?

编辑,这是堆栈跟踪>> http://pastebin.com/2c5XBCmG

EDIT here's the stack trace >> http://pastebin.com/2c5XBCmG

推荐答案

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

The items attribute is typically populated with a collection or array of item objects. itemValue and itemLabel simply refer to bean properties of those item objects, if specified; otherwise, the item objects themselves will be stringified. Alternatively, you may specify a Map of items, in which case the map keys are interpreted as option values and the map values correspond to option labels. If itemValue and/or itemLabel happen to be specified as well, the item value property will apply to the map key and the item label property will apply to the map value.

简而言之,如果您需要使用自定义Bean列表"作为items属性,则还需要使用itemValueitemLabel属性.就个人而言,我更喜欢使用Maps-LinkedHashMap实例来具体地填充我的选择标签,但这只是一个问题.

In a nutshell, if you need to use a List of your Custom Beans as the items attribute you need to use also the itemValue and itemLabel attributes. Personally, I'll prefer using Maps -LinkedHashMap instances speciffically- for populating my select tags, but that's a matter of taste.

改编自Spring文档中的示例,您的代码应如下所示:

Adapting an example from the Spring Documentation, your code should look like this:

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

我将company属性用作itemValueitemLabel,但是您可以自由选择适合您要求的属性.

I'm using the company attribute as both itemValue and itemLabel, but you're free to choose the attributes that fit your requirements.

这篇关于SpringMVC form:options items属性:究竟是什么期望?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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