【问题标题】:Spring MVC: PropertyEditor throwing exception, unable to set valuesSpring MVC:PropertyEditor 抛出异常,无法设置值
【发布时间】:2014-05-11 06:26:39
【问题描述】:

我是 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

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

请告诉我如何解决这个问题。

【问题讨论】:

    标签: java spring spring-mvc web-applications propertyeditor


    【解决方案1】:

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

    org.springframework.beans.NotReadablePropertyException:bean 类 [com.xxx.model.Employee] 的无效属性“员工”:bean 属性“员工”不可读或具有无效的 getter 方法: getter的返回类型和setter的参数类型是否匹配?

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

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

    直接访问address 字段即可。其实访问city字段里面的Address address字段:

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

    【讨论】:

    • 还有一件事要问:在这里我看不到 PropertyEditor 的任何用途 - 正确吗?
    猜你喜欢
    • 2016-03-30
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2016-09-26
    相关资源
    最近更新 更多