javaspringspring-mvcweb-applicationspropertyeditor

Spring MVC: PropertyEditor throwing exception, unable to set values


I am new to Spring 3 MVC and was trying to implement PropertyEditor. Below is the code I tried:

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>

enter.jsp file

<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";
}

Problem:

The form.jsp file is not getting rendered, I am getting an error:

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?

The error is thrown in this line of the JSP file:

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

Please let me know how to resolve this.


Solution

  • From the error message (emphasis mine):

    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?

    The error seems very specific: there's no field employee in class Employee. You're trying to access to address field by using employee.address:

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

    Just access to address field directly. In fact, access to city field inside Address address field:

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