Spring MVC:将复杂对象绑定到UI [英] Spring MVC: Binding complex Objects to UI

查看:125
本文介绍了Spring MVC:将复杂对象绑定到UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的用户对象,我试图用Spring MVC绑定到UI。用户对象中有另一个对象地址。

I have my User Object which I am trying to bind to UI with spring MVC. User Object has another object Address in it.

public class User {
    String firstName;
    String lastName;
    String userName;
    Address address;
}

地址obj。

public class Address {
    String street;
    String House;
    String country;
}

当我将用户对象绑定到UI时说对于编辑用户功能我只想要保留firstName和lastName。

When I bind user object to UI say for edit user functionality I only want to keep firstName and lastName.

现在我的第一个问题是,如果我不将用户的其余属性保留为隐藏字段,那么我将获得这些值为null。这意味着UI上的绑定对象是User对象的新实例。我不能处理同一个用户对象并将其绑定到UI上,只获取旧值的更新值吗?

Now my first question if I don't keep rest of the properties of the User as hidden field I am getting those values null. It means the binding object on UI is a new instance of the User object.. Can't I work on the same user object and bind it on UI and get only updated values with old value?

其次:通过隐藏字段方法我没有得到Address对象,我得到null

Secondly: by hidden field method I am not getting Address object back, I am getting null for that

<form:hidden path="user.address" />

其中user是我的模态属性。

Where user is my modal attribute.

我不知道,我对Spring MVC的理解可能存在差距。对于UI上的绑定对象,我们总是要创建新实例吗?如果嵌套我们如何在绑定的模态属性中获得嵌套对象(地址)?

I don't know, there might be a gap my understanding of Spring MVC. For binding object on UI do we always have to create new instance? If nested how do we get nested objects (Address) back in binded modal attributes ?

一旦选项我猜是使用绑定器。但据我所知,binder还将使用id获取对象??请给出解决方法,我可以保存我的查询以获取该对象

Once option I guess is using binders. But as far as I understand, binder will also fetch object using id?? Kindly give the workaround where I can save my query to fetch that object back

推荐答案

获取地址对象返回,您需要列出每个单独的属性:例如:

To get your Address object back, you will need to list out each of the individual properties: for ex:

<form:hidden path="user.address.street" />
<form:hidden path="user.address.country" />

依此类推。

现在,绑定到UI的对象是从控制器传递的对象。我假设在提交表单时,您希望将 User 对象作为 @ModelAttribute 返回。在这种情况下,您有两个选择:第一个是您需要在客户端列出用户对象的所有属性(作为隐藏对象或作为可见的表单元素)。

Now, the object that you bind to the UI is the object that you passed from your controller. I am assuming that When the form gets submitted you want to get the User object back as a @ModelAttribute. In that case, you have two options: the first is that you will need to list out all the properties of your User object on the client-side (either as hidden objects or as visible form-elements).

第二个选项是:您只在客户端显示所需的属性,而不是将所有其他属性列为隐藏字段,您只将用户实例的 id 保留为隐藏字段。现在,当您在表单提交时收到此 @ModelAttribute 时,您可以使用 id 并在调用更新之前设置更新的属性。

The second options is: you only display the required properties on the client-side, and rather than listing all the other propeties as hidden fields, you only keep the id of your User instance as a hidden field. Now, when you receive this @ModelAttribute upon form-submission, you can load the whole object from the database using the id and set the updated properties on it before calling update.

ex:

@RequestMapping("updateUser.do")
public String updateUser(@ModelAttribute User user, ... /* rest of the args */) {
    // ... some other code
    User existingUser = userService.findById(user.getId());
    existingUser.setFirstName(user.getFirstName());
    existingUser.setLasstName(user.getLastName());

    userService.update(existingUser);
    // ... rest of the code
}

没有您只能在客户端的必需属性上工作,并将其与同一个现有对象合并的方式。

There is no way that you can work only on the required-properties on the client-side, and merge it with that same existing object.

这篇关于Spring MVC:将复杂对象绑定到UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆