form:options tag cause error : Bean property is not readable or has an invalid getter method
I want to use the form:options provided by Spring to provide choices.
My JSP implemented with Spring 2.5 is
<td><form:select path="billingType">
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="billingType" itemLabel="billingTypeName" />
</form:select>
</td>
My AccountCommand.java is
private int billingType;
private String billingTypeName;
public int getBillingType() {
return billingType;
}
public void setBillingType(int billingType) {
this.billingType = billingType;
}
private Map<String, Integer> billingTypeChoice = new HashMap<String, Integer>() {
{
put("Monthly", 1);
put("Block", 2);
put("Per Use", 3);
开发者_StackOverflow中文版}
};
public Map<String, Integer> getbillingTypeChoice() {
return billingTypeChoice;
}
public void setbillingTypeChoice(Map<String, Integer> billingTypeChoice) {
this.billingTypeChoice = billingTypeChoice;
}
public String getBillingTypeName() {
return billingTypeName;
}
public void setBillingTypeName(String billingTypeName) {
this.billingTypeName = billingTypeName;
}
My Eclipse console is:
15:55:23,140 ERROR org.springframework.web.servlet.tags.form.OptionsTag:84 - Invalid property 'billingType' of bean class [java.lang.String]: Bean property 'billingType' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.NotReadablePropertyException: Invalid property 'billingType' of bean class [java.lang.String]: Bean property 'billingType' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:540)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:532)
at org.springframework.web.servlet.tags.form.OptionWriter.renderFromMap(OptionWriter.java:164)
...
When you do:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="billingType" itemLabel="billingTypeName" />
you are saying go to addAccountCommand.billingTypeChoice which is a Map and do a getBillingType() for the value and a getBillingTypeName() for the label. As these methods are not defined on the map you get the error. You should use getKey() and getValue() on the map.As you have it defined now it should be:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="key" itemLabel="value" />
because you have defined the Map in a very strange way. It's usually Map because I suppose the key is the Integer and the value the String.
Hope it helps.
I just tried it on Spring 2.5. Just code below should work.
<form:select path="billingType">
<form:options items="${addAccountCommand.billingTypeChoice}" />
</form:select>
精彩评论