Keep value in model attribute for spring mvc
Asked Answered
M

3

18

When I'm using a Spring MVC controller and I submit a form by setting value to the modelAttribute I lose some of the model's fields which are not mapped in the form. In this example, because I didn't map age and address, these user's fields are lost and so they have null value. I don't want to let the user change their age and the address, that's why these fields are not in the form.

Method controller to post user edit form:

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String editionUser(Model model, @ModelAttribute("accountForm") User user,
  BindingResult bresult,
  RedirectAttributes redirectAttributes) {
  //user.getAge() and user.getAddress are null           

  //Save the information...
}

Method controller to get edit user page:

@RequestMapping(value = "/edit", method = RequestMethod.GET)
public String initEdit(Model model) {
  // the user fields are all filled
  User user = userService.getUserById(...);
  model.addAttribute("accountForm", user);
  return "edition";
}


class User {
  Long id;
  String email;
  String age;
  String address;
  String nickname;

  // ...
}

edition.jsp

<form:form class="form" method="POST" action="edition" modelAttribute="accountForm">
  <form:label path="email">email</form:label>
  <form:input path="email"  name='email'/>
  <form:label path="nickname">nickname</form:label>
  <form:input path="nickname"  name='nickname'/>
  <form:hidden path="id" />
  <input name="submit" type="submit"    value="valid" />  
</form:form>

What are the best solution to not lose the value of these fields ? (age and address)

Use form hidden path for each field ?? Store the user in session before redirect to the edit page and set retreive the user session in post method controller to only change the modified fields ? Is there a generic method?

Maid answered 13/11, 2013 at 13:55 Comment(0)
U
8

I'd go with session for one simple reason:
hidden form fields can be easily manipulated client-side before submitting the form, therefore they do not satisfy your requirements.

Note that without saving previously entered values somewhere, you can't even detect if they were tampered with on client-side, so hidden fields are definitely not an option.
Use the session.

Umbles answered 13/11, 2013 at 14:36 Comment(3)
you are right about the hidden field :) instead to retreive by session i can get the user by my user service too ? (service call dao etc.. to retreive user from database)Maid
if I understood correctly, you want to sacve object in DB and retrieve on every edit... this is possible, but not effective nor clean.Loralyn
I don't save the object each time, i just retreive the user just before save the user, first i retreive the object by my service and then i set the changed field and finally i save the object. so this way i don't need session but maybe the performance are better by sessionMaid
I
4

It is an old post, but maybe the answer will help Spring newbies like me

Unfortunately unless you use @SessionAttributes({"attributeName"}) you will lose the other attributes of your object.

This post explains it Spring MVC - passing variables from one page to anther

Iridic answered 4/7, 2014 at 18:13 Comment(0)
V
2

See my answer here:

Is it possible to update only a subset of attributes on an entity using Spring MVC with JPA?

This should do the job for you. Unfortulately there is no more elegant method.

On a side note, it seems like Spring WebFlow manages to update a model with only the fields from the post. Probably it stores the Model in the session, and updates only the values contained in the POST.

Vacua answered 10/1, 2014 at 14:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.