我是Spring3MVC的新手,并试图实现PropertyEditor。下面是我尝试过的代码:
Employee.java
private String name;
private String gender;
private Address address;
public Employee() {
// TODO Auto-generated constructor stub
}
public Employee(String name,String gender,Address address) {
this.name = name;
this.gender = gender;
this.address = address;
}
//Getters and SettersAddress.java
private String city;
public Address() {}
public Address(String city/*,String state*/) {
this.city = city;
}
// Getters and SettersAddressTypeEditor.java
public class AddressTypeEditor extends PropertyEditorSupport {
@Override
public void setAsText(String text) throws IllegalArgumentException {
Address type = new Address(text.toUpperCase());
setValue(type);
}
}Context.xml
<mvc:annotation-driven />
<context:component-scan
base-package="com.XXX" />
<bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="com.xxx.model.Address" value="com.xxx.editor.AddressTypeEditor"/>
</map>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>enter.jsp文件
<form:form action="input" commandName="employee" method="post">
<table>
<tr>
<td>Name: </td>
<td>
<form:input path="name"/>
</td>
</tr>
<tr>
<td>Gender: </td>
<td>
<form:input path="gender"/>
</td>
</tr>
<tr>
<td>City: </td>
<td>
<form:input path="employee.address"/>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Submit">
</td>
</tr>
</table>
</form:form>Controller.java
// Clicking the index.jsp <a></a>, the above posted JSP file is displayed.
@RequestMapping(value="enter")
public String enterForm(ModelMap model){
model.addAttribute("employee",new Employee());
return "form";
}
@RequestMapping(value="input")
public String inputForm(Employee employee){
System.out.println(employee.getName());
System.out.println(employee.getGender());
Address address = employee.getAddress();
System.out.println(address.getCity());
return "success";
}问题:
没有呈现form.jsp文件,我收到了一个错误:
org.springframework.beans.NotReadablePropertyException: Invalid property 'employee' of bean class [com.xxx.model.Employee]: Bean property 'employee' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?在JSP文件的这一行中引发错误:
<tr>
<td>City: </td>
<td>
<form:input path="employee.address"/>
</td>
</tr>请告诉我如何解决这个问题。
发布于 2014-05-11 06:29:47
从错误信息(强调我的):
org.springframework.beans.NotReadablePropertyException:无效属性com.xxx.model.Employee:bean属性'employee‘不可读或具有无效的getter方法: getter的返回类型与setter的参数类型匹配吗?
错误似乎非常具体:在类employee中没有字段Employee。您正在尝试通过使用address访问employee.address字段
<form:input path="employee.address"/>直接访问address字段即可。实际上,在city字段中访问Address address字段:
<form:input path="address.city"/>https://stackoverflow.com/questions/23589639
复制相似问题