Zack
Zack

Reputation: 5148

Spring MVC Drop down box throws error

I followed the steps in this question and I am still running into problems.

My Controller looks as follows...

@RequestMapping(value = "listBooks.htm")
public String goToNextPage(Model model, HttpServletRequest request){

    HashMap<String,Map<String,String>> hashMapOfData = new HashMap<String,Map<String,String>>();

    Map<String,String> m = new LinkedHashMap<String,String>();
    m.put("1", "foo");
    m.put("2", "bar");
    hashMapOfData.put("m", m);
    model.addAttribute("dropdownData", hashMapOfData);
.....
}

My jsp file...

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:select path="dropdownData"><br />
 <form:option label="Select..." value=""/>
 <form:options items="${dropdownData}" itemLabel="label" itemValue="value"/>
</form:select>

I get the error...

org.springframework.beans.NotReadablePropertyException: Invalid property 'value' of bean class [java.lang.String]: Bean property 'value' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter

BUT if I change my jsp file to... removing the itemLabel="label" itemValue="value"

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:select path="dropdownData"><br />
 <form:option label="Select..." value=""/>
 <form:options items="${dropdownData}"/>
</form:select>

The page does load, but the drop down box selections are

Can anyone help me out? I am quite a novice when it comes to Spring MVC so very detailed answered would be much appreciated.

Upvotes: 0

Views: 1040

Answers (1)

dardo
dardo

Reputation: 4970

The error describes it all

org.springframework.beans.NotReadablePropertyException: Invalid property 'value' of bean class [java.lang.String]: Bean property 'value' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter

Spring is trying to find a getter for value within the java.lang.String, something that doesn't exist.

I'd just use a List<LabelValueModel> (LabelValueModel being something you make yourself, which has the properties for label and value.

Your final jsp would look like this then:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<form:select path="dropdownData"><br />
 <form:option label="Select..." value=""/>
 <form:options items="${dropdownData}" itemLabel="label" itemValue="value"/>
</form:select>

LabelValueModel would just be a simple POJO to avoid binding to a specific technology:

public class LabelValueModel
{
  private String label;
  private String value;

  ..public getters..
  ..might consider private setters and only allowing setting through constructor..
}

Upvotes: 3

Related Questions