Spring MVC 的问题。如何从两个或多个对象创建视图?

发布于 2024-10-17 08:27:48 字数 1484 浏览 3 评论 0原文

[春季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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰魂雪魄 2024-10-24 08:27:48

将对象包装在包装器表单类中并将其传递到模型中。

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

那么

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

在您的模型中,地址应该附加到用户吗?换句话说,在我看来,UserAddress 具有一对多关系,您应该让数据访问层处理这些问题。

@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;
}

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;
}
西瓜 2024-10-24 08:27:48

您还可以执行以下操作,这将阻止您严格出于显示目的引入新类。 Spring 表单可以处理嵌套属性。

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

在 .jsp 中,您可以通过以下方式到达地址对象:

<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>

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>
鹤舞 2024-10-24 08:27:48

对象 user 的属性是地址(我想你有一个名为 getAddress() 的 getter ),所以在表单中你必须使用 address 而不是 adr:

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

或创建一个名为 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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文