1

I am working on a Spring MVC based application. The process flow is as follows:

  1. Fetch the data from DB (table mapped to a POJO)
  2. Display a form backed by the POJO (from step 1). Not all the fields are displayed (like Primary Key etc).
  3. User can update some field value in the form and will then submit.

On receving the updated POJO using @ModelAttribute annotation in my Controller, I found that not all the fields are populated in the POJO received via @ModelAttribute. All the fields which were not mapped on the JSP (like primary key) are set to null or their default value in case of primitives. Due to this I am not able to update the same in the DB.

One solution that I found is I can use fields but that does not sound much efficient solution as I have a large number of attributes which are not displayed on the JSP page.

2
  • What do you mean "You don't want to use fields"? You mean hidden fields? Commented Aug 2, 2014 at 7:25
  • Yes I mean the hidden fields for all those attributes which are not being displayed like primary key of the record. Commented Aug 3, 2014 at 8:41

3 Answers 3

1

A model attribute is simply a glorified request attribute. Request attributes only live for the duration of one request-response cycle.

HTTP request -> Get POJO from DB -> Add POJO to model attributes -> Render JSP -> HTTP response

After that, the request attributes are eventually GC'ed since they are no longer reachable by the application (the servlet container makes sure of that).

The next request you send will have its set of new request attributes with no relation to the previous requests'.

When you generate a <form> from a model attribute, Spring creates the <input> elements from the fields of the model attribute which you choose. When you eventually submit the form, only those values will be sent as request parameters in the request. Your application will therefore only have access to those to generate the new model attribute.

You seem to need Session attributes or Flash attributes (which are really just short-lived session attributes).

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please elaborate the same with some code snippet. Thanks
@Sumit I don't have a code snippet to add. Look for Spring's RedirectAttributes (that's their implementation of flash attributes) and Servlet's HttpSession (and setAttribute(..)).
0

Please if somebody know a better solution please let me know, but on my application we are always sending back all those id´s and others values that we want to persist in the request response with hidden fields, but I think is a little bit risk, for example in case of id´s. which could be used for SQLInjections attacks.

Comments

0

You could use path variable to transport the primary key (kind of rest url ...) and make use of all the magic of Spring :

  • create a DefaultFormattingConversionService bean (to keep default conversions)
  • register (in that ConversionService) a Converter converting a String in your POJO (string -> primary key -> object from database)
  • directly use the path variable in your controller methods

    @RequestMapping(value=".../{data}/edit", method=RequestMethod.GET) public String edit(@ModelAttribute("data") Pojo pojo) { return "view_with_form"; } @RequestMapping(value=".../{data}/edit", method=RequestMethod.POST) public String update(@ModelAttribute("data") Pojo pojo, BindingResult result) { if (result.hasErrors()) { return "view_with_form"; } return "redirect:.../data/edit"; } 

When you give a ModelAttribute to a controller method, Spring tries to find it in the session or in a path variable. And even if you didn't asked for it, the error management is not very expensive ...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.