与 Spring MVC JSP 页面绑定混淆

发布于 2024-10-20 13:04:06 字数 939 浏览 3 评论 0原文

我一直无法解决我的绑定问题。

我有一个类 Person

Class Person {

Private fname;
private lname;
public Address address;

另一个类

class Address {

    private street;
    private suburb;
}

现在控制器中的

model.addAttribute("personAttribute", new Person());

我在 jsp 页面中有

<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
<td><form:input path="firstName"/></td>

Uptill 这个 evrrythng 是好的,但是当我添加

<td><form:input path="street"/></td>

然后就成为问题。我不知道如何将 Adress 对象与 person 添加。

我的意思是我必须做什么

    model.addAttribute("personAttribute", new Person(new Address()));
    or

    model.addAttribute("addressAttribute", new Address());
or

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

I have not been able to solve my binding problem.

I have one class Person

Class Person {

Private fname;
private lname;
public Address address;

Now one other class

class Address {

    private street;
    private suburb;
}

in controller i have

model.addAttribute("personAttribute", new Person());

IN jsp page i have

<form:form modelAttribute="personAttribute" method="POST" action="${saveUrl}">
<td><form:input path="firstName"/></td>

Uptill this evrrythng is ok but when i add

<td><form:input path="street"/></td>

Then becomes the problem . i don't know how can i add Adress object with person.

i mean do i have to do

    model.addAttribute("personAttribute", new Person(new Address()));
    or

    model.addAttribute("addressAttribute", new Address());
or

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

童话里做英雄 2024-10-27 13:04:06

你必须像这样绑定路径:

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

这是因为你在绑定中的 Person 对象的范围内。

you have to bind path like that :

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

It's because you are in scope of Person object in binding.

转身以后 2024-10-27 13:04:06

以下代码是正确的:

model.addAttribute("personAttribute", new Person());

这将用空属性(包括空地址)初始化 Person。

如果要确保地址不为空,则必须传递包含值的 person 实例。例如

Person person = new Person();
person.setFirstName("John");
person.setLastName("Smith");

Address address = new Address();
address.setStreet("#10 Avenue");
address.setSuburb("Cook");

person.setAddress(address);

,通常您可能希望使用从数据库或数据源检索的值来填充人员对象。

The following code is correct:

model.addAttribute("personAttribute", new Person());

This will initialize the Person with empty attributes, including an empty Address.

If you want to make sure that the address is not empty, you have to pass a person instance that contains a value. For example

Person person = new Person();
person.setFirstName("John");
person.setLastName("Smith");

Address address = new Address();
address.setStreet("#10 Avenue");
address.setSuburb("Cook");

person.setAddress(address);

Normally you may want to populate the person object with a value retrieved from a database or data source.

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