Keep value in model attribute for spring mvc

19,122

Solution 1

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.

Solution 2

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

Solution 3

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.

Share:
19,122

Related videos on Youtube

julus
Author by

julus

Updated on September 14, 2022

Comments

  • julus
    julus over 1 year

    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?