开发者

Problem with Spring MVC. How can I create view from two or more object?

[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&g开发者_运维技巧t;
    <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.

public class MyForm
{
   public user;
   public address;
   // getters, setters, etc.
}

Then

ModelAndView mav = new ModelAndView(); // ModelAndView
mav.addObject("myForm", new MyForm()); // e.g.

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 to Address, and you should let your data access layer handle these concerns.

@Entity
@Table(name = "user")
public class User
{
   @Id @Column(name="user_id")
   public Long id;

   @OneToMany
   @JoinColumn(name = "user_id") // so the address table would have a user_id foreign key
   public Address address;
}


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.

User user = new User();
Address adr = new Address();
user.setAddress(adr);
mav.addObject("user", user);

In your .jsp you'd reach the address object this way:

<form:form method="POST" commandName="user">
<form:label path="name" />
<form:input path="name" />
<form:label path="address.street" />
<form:input path="address.street" />
</form:form>


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:

<form:label path="address.street" />
<form:input path="address.street" />

or create a getter called getAdr() (as you want)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜