【发布时间】:2011-10-09 11:04:23
【问题描述】:
我的一个对象中有一个三态 Boolean 属性(true、false 和 null),但我不知道如何使用 Spring Forms 标记正确绑定它。我想使用一系列 3 个单选按钮(true、false 和 null),但 Spring 似乎不喜欢我目前尝试的。
这是支持 POJO:
public class Spirit {
/*** Private Fields **/
private Integer id;
private String name;
private Boolean isAlive;
/*** Constructor **/
public Spirit() {}
/*** Getters **/
public Integer getId() {return id;}
public String getName() {return name;}
public Boolean isAlive() {return isAlive;}
/*** Setters **/
public void setId(Integer id) {this.id = id;}
public void setName(String name) {this.name = name;}
public void isAlive(Boolean isAlive) {this.isAlive = isAlive;}
}
这是我正在使用的表格(不起作用):
<sf:form method="POST" modelAttribute="spirit">
<table>
<tr>
<th><label for="spirit_name">Name</label></th>
<td><sf:input path="name" id="spirit_name" /></td>
</tr>
<tr>
<th><label for="spirit_isAlive">Livelyness</label></th>
<td>N/A: <sf:radiobutton path="isalive" value="null" /> Alive: <sf:radiobutton path="isalive" value="true" /> Dead: <sf:radiobutton path="isalive" value="false" /></td>
</tr>
</table>
<sf:hidden path="id"/>
<input type="submit" value="save" />
</sf:form>
我得到的错误:
SEVERE: Servlet.service() for servlet jsp threw exception
org.springframework.beans.NotReadablePropertyException: Invalid property 'isAlive' of bean class [com.example.Spirit]: Bean property 'isAlive' 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:707)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:699)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:98)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:224)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
at org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:78)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
at org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:82)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
at org.apache.jsp.WEB_002dINF.views.spirits.edit_jsp._jspx_meth_sf_005fradiobutton_005f0(edit_jsp.java:523)
at org.apache.jsp.WEB_002dINF.views.spirits.edit_jsp._jspService(edit_jsp.java:201)
another question 非常相似,但这种情况下的解决方案是简单地将类型更改为 boolean,而不是 Boolean。不幸的是,更改三态的 get/set 方法只能作为最后的选择。我做错了什么?
【问题讨论】:
标签: java forms spring spring-mvc binding