Spring MVC 的问题。如何从两个或多个对象创建视图?
[春季3.0.5] [jboss 5.1]
A 在控制器中有两个类,
public class User {
private String name;
private String surname;
private Address address;
...
sets and gets
setters and getters
}
public class Address {
private String street;
...
setters and getters
}
我有以下代码:
@Controller
public class MyController {
@RequestMapping(value = "/index")
public ModelAndView showForm() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
User user = new User();
Address adr = new Address();
mav.addObject("user", user);
mav.addObject("adr", adr);
}
现在我想在 JSP 中使用两个输入元素创建
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html><head><body>
<form:form method="POST" commandName="user">
<form:label path="name" />
<form:input path="name" />
<form:label path="adr.street" />
<form:input path="adr.street" />
</form:form>
</body>
</html>
当我运行时遇到如下异常:
org.springframework.beans.NotReadablePropertyException: Invalid property bean 类 [form.User] 的“adr”:Bean 属性“adr”不可读或具有无效的 getter 方法:getter 的返回类型是否与 setter 的参数类型匹配? org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707) org.springframework.be
有人可以向我解释一下为什么以及如何改进代码吗?
[spring 3.0.5]
[jboss 5.1]
A have a two class
public class User {
private String name;
private String surname;
private Address address;
...
sets and gets
setters and getters
}
public class Address {
private String street;
...
setters and getters
}
In Controller I have this code:
@Controller
public class MyController {
@RequestMapping(value = "/index")
public ModelAndView showForm() {
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
User user = new User();
Address adr = new Address();
mav.addObject("user", user);
mav.addObject("adr", adr);
}
And now I want to create from with two input element in JSP
<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html><head><body>
<form:form method="POST" commandName="user">
<form:label path="name" />
<form:input path="name" />
<form:label path="adr.street" />
<form:input path="adr.street" />
</form:form>
</body>
</html>
When I runing a got a exception like this one:
org.springframework.beans.NotReadablePropertyException: Invalid property 'adr' of bean class [form.User]: Bean property 'adr' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
org.springframework.be
Can someone please explain to me why and how to improve the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将对象包装在包装器表单类中并将其传递到模型中。
那么
在您的模型中,地址应该附加到用户吗?换句话说,在我看来,
User
与Address
具有一对多关系,您应该让数据访问层处理这些问题。Wrap your objects in a wrapper form class and pass it in the model.
Then
In your model, should address be attached to a user? In other words, it seems to me like a
User
has a one to many relationship toAddress
, and you should let your data access layer handle these concerns.您还可以执行以下操作,这将阻止您严格出于显示目的引入新类。 Spring 表单可以处理嵌套属性。
在 .jsp 中,您可以通过以下方式到达地址对象:
You could also do the following, which would prevent you from introducing a new class strictly for display purposes. Spring forms can handle nested properties.
In your .jsp you'd reach the address object this way:
对象 user 的属性是地址(我想你有一个名为 getAddress() 的 getter ),所以在表单中你必须使用 address 而不是 adr:
或创建一个名为 getAdr() 的 getter (如你所愿)
The property of the object user is address (I suppose you have a getter called getAddress()), so in the form you have to use address instead of adr:
or create a getter called getAdr() (as you want)