Smurf smurf Smurf smurf - 1 year ago 47
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:


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


Answer Source

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

public String toString(){
    return getName();

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