Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep value in model attribute for spring mvc

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?

like image 519
julus Avatar asked Nov 13 '13 13:11

julus


People also ask

What is model attribute in Spring MVC?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

What is difference between @RequestBody and @ModelAttribute?

@ModelAttribute is used for binding data from request param (in key value pairs), but @RequestBody is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.

Can we use model attribute as method argument?

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. So in this case we expect that we have in the Model person object as key and we want to get its value and put it to the method argument Person person.

Can we map a request with model attribute?

Method level ModelAttribute annotation cannot be mapped directly with any request. Let's take a look at the following example for better understanding. We have 2 different methods in the above example.


2 Answers

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.

like image 198
Michał Rybak Avatar answered Sep 21 '22 21:09

Michał Rybak


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

like image 33
Shady Hussein Avatar answered Sep 17 '22 21:09

Shady Hussein