SpringMVC form:options items attribute: what exactly is it expecting?

有些话、适合烂在心里 提交于 2019-12-21 07:31:04

问题


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

It says the items attribute is for

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

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>

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

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

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

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)

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> 

to accomplish what I'm doing?

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


回答1:


The Spring Documentation says this about the items attribute of the form:options tag:

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.

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.

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>

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




回答2:


Usualy I am doing it with spring tag like this :

<springform:select path="myObjectList" id="selected_company">
    <springform:option value="0" label="--- Select One ---"></springform:option>
    <springform:options items="${myObjectList}" itemValue="company" itemLabel="company"></springform:options>
</springform:select>

don't forget including the namespace declaration : xmlns:springform="http://www.springframework.org/tags/form"



来源:https://stackoverflow.com/questions/15526425/springmvc-formoptions-items-attribute-what-exactly-is-it-expecting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!