Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC: Binding complex Objects to 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;
}

Address obj.

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

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

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?

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

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

Where user is my modal attribute.

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 ?

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

like image 420
Dhruv Bansal Avatar asked Jun 03 '13 11:06

Dhruv Bansal


1 Answers

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

and so on.

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

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.

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

like image 189
Bhashit Parikh Avatar answered Oct 11 '22 08:10

Bhashit Parikh