我想使用Spring提供的表单来提供选择。
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>
我的AccountCommand.java是
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);
}
};
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;
}
我的Eclipse控制台是:
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)
...
答案 0 :(得分:3)
当你这样做时:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="billingType" itemLabel="billingTypeName" />
你要说的是addAccountCommand.billingTypeChoice,这是一个Map,为值做一个getBillingType(),为标签做一个getBillingTypeName()。由于这些方法未在地图上定义,因此您会收到错误。你应该在地图上使用getKey()和getValue()。如你现在定义它应该是:
<form:options items="${addAccountCommand.billingTypeChoice}" itemValue="key" itemLabel="value" />
因为您以非常奇怪的方式定义了地图。它通常是Map,因为我认为键是Integer,值是String。
希望它有所帮助。
答案 1 :(得分:0)
我刚刚在Spring 2.5上尝试过它。只需下面的代码即可。
<form:select path="billingType">
<form:options items="${addAccountCommand.billingTypeChoice}" />
</form:select>