gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:59:52 24 4
gpt4 key购买 nike

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com