java - Spring MVC : PropertyEditor throwing exception, 无法设置值

标签 java spring spring-mvc web-applications propertyeditor

我是 Spring 3 MVC 的新手,正在尝试实现 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 Setters

地址.java

private String city;

public Address() {}

public Address(String city/*,String state*/) {
    this.city = city;
}

// Getters and Setters

AddressTypeEditor.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>

输入.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>

请告诉我如何解决此问题。

最佳答案

来自错误消息(强调我的):

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?

该错误似乎非常具体:Employee 类中没有字段 employee。您正在尝试使用 employee.address 访问 address 字段:

<form:input path="employee.address"/>

只需直接访问地址字段即可。事实上,访问Address地址字段中的city字段:

<form:input path="address.city"/>

关于java - Spring MVC : PropertyEditor throwing exception, 无法设置值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23589639/

相关文章:

java - Spring MVC 测试中没有定义 [javax.persistence.EntityManagerFactory] ​​类型的合格 bean 问题

java - JTable 中的重音在 Eclipse 中显示,但在 .jar 中不显示

java - spring+hibernate 设置不允许保存

java - 如何使用 @responsebody 在 JSON 返回值中添加字段名称?

spring - 第一个 Spring 批处理作业被第二个作业的详细信息覆盖

java - java PermGen 空间是虚拟机总内存的一部分吗?

java - 重新验证 jframe 的问题

json - 带有 JSON 的 Spring MVC 多部分请求

java - Spring Boot JSP 错误 : NoClassDefFoundError

model-view-controller - 如何重写ResourceHttpRequestHandler来实现自定义的资源处理程序?