Smurf smurf Smurf smurf - 1 month ago 6
Java Question

SpringMVC dropdown box displaying object information rather than values

I have a model attribute set to a list of product objects. However when I try to access the list in a dropdown box form like this:

<td><form:select path="product" items="${productList}"/></td>


It displays information about the object itself, rather than the product name:

Product@4c23d7e2


How can I make the form display the names of each element in productList, like? Like the first element would be

productList.get(0).getName()

Answer

One way would be to override the toString() method in Product as follows:

@Override
public String toString(){
    return getName();
}

Another would be to pass a List<String> of product names instead of List<Product>.