首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring : PropertyEditor抛出异常,无法设置值

Spring : PropertyEditor抛出异常,无法设置值
EN

Stack Overflow用户
提问于 2014-05-11 06:26:39
回答 1查看 499关注 0票数 0

我是Spring3MVC的新手,并试图实现PropertyEditor。下面是我尝试过的代码:

Employee.java

代码语言:javascript
复制
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

代码语言:javascript
复制
private String city;

public Address() {}

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

// Getters and Setters

AddressTypeEditor.java

代码语言:javascript
复制
public class AddressTypeEditor extends PropertyEditorSupport {

@Override   
public void setAsText(String text) throws IllegalArgumentException {
    Address type = new Address(text.toUpperCase());
    setValue(type);
}
}

Context.xml

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
<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

代码语言:javascript
复制
// 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文件,我收到了一个错误:

代码语言:javascript
复制
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文件的这一行中引发错误:

代码语言:javascript
复制
<tr>
            <td>City: </td>
            <td>
                <form:input path="employee.address"/>
            </td>
        </tr>

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-05-11 06:29:47

从错误信息(强调我的):

org.springframework.beans.NotReadablePropertyException:无效属性com.xxx.model.Employee:bean属性'employee‘不可读或具有无效的getter方法: getter的返回类型与setter的参数类型匹配吗?

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

代码语言:javascript
复制
<form:input path="employee.address"/>

直接访问address字段即可。实际上,在city字段中访问Address address字段:

代码语言:javascript
复制
<form:input path="address.city"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23589639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档