[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?
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
Userhas a one to many relationship toAddress, and you should let your data access layer handle these concerns.