Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 455
Edge D-Vort Avatar asked Mar 20 '13 14:03

Edge D-Vort


People also ask

What is Model attribute in form tag?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

Which of the following attributes of spring form tag is used to expose the binding path?

Spring tag Library 1) The 'form' tag: The 'form' tag renders an HTML 'form' tag. It exposes a binding path to inner tags for binding the data entered. It puts the command object in the PageContext so that the command object can be accessed by all the inner tags.

What is the correct syntax to generate a drop down box in Spring MVC?

To render a list box, just add the “multiple=true” attribute in the select tag. HTML code, with a hidden value to handle the country selections. For dropdown box, list box or “select” options, as long as the “path” or “property” is equal to the “select option key value“, the options will be selected automatically.


2 Answers

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.

like image 83
Carlos Gavidia-Calderon Avatar answered Oct 12 '22 13:10

Carlos Gavidia-Calderon


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"

like image 37
jpprade Avatar answered Oct 12 '22 11:10

jpprade