jacekn
jacekn

Reputation: 1541

Spring form backing object issue

I'm struggling with the way Spring handles form backing object during a POST. ScreenObject property that has a corresponding input tag in the html form survives the post, as expected. But if a property does not have an input tag, two scenarios occur:

  1. If property is passed in as a request parameter, it survives the post.
  2. If property is not passed in as a request parameter, it does not survive the post.

Here's my code.

screen object

private Integer hostSiteSectionId; // no input tag but survives if passed as a request param
private String name;  // input tag so survives
private String orderFactor; // input tag so survives

private Integer hostSiteId; // no input tag but survives if passed as a request param
private String hostSiteName; // no input tag and not passed as request param so does not survive

GET

@RequestMapping(method=RequestMethod.GET)
public ModelAndView edit(@RequestParam(value="hostSiteId", required=false) Integer hostSiteId, @RequestParam(value="hostSiteSectionId", required=false) Integer hostSiteSectionId, Locale locale) {
    HostSiteSectionHeaderEditScreenObject screenObject=new HostSiteSectionHeaderEditScreenObject();

    initializeScreenObject(hostSiteId, hostSiteSectionId, screenObject, locale, true);

    ModelAndView modelAndView=new ModelAndView();
    modelAndView.addObject("screenObject", screenObject);
    modelAndView.setViewName(WebView.HOST_SITE_SECTION_HEADER_EDIT_PAGE.getViewName());
    return modelAndView;    
}

POST with CANCEL

@RequestMapping(method=RequestMethod.POST, params="cancel")
public String cancel(@ModelAttribute("screenObject") HostSiteSectionHeaderEditScreenObject screenObject) {
    // logic that returns redirect
}

My initializeScreenObject() method only sets properties of the screenObject. It doesn't operate on the model. I don't see how it would interfere, so I'm not posting it's basic code.

Within this post, and it works the same in other posts, screenObject has the following:

  1. All input provided by user in the form via input tags is present. No issue.
  2. hostSiteId (no input tag) is present in the screenObject only if getter url included it as parameter (edit?hostSiteId=2 for example)
  3. hostSiteSectionId (no input tag) is present in the screenObject only if getter url included it as parameter (edit?hostSiteSectionId=2 for example)
  4. All other properties that have no corresponding input tags and are not passed in as request params are null.

To illustrate further #4. I have a screenObject.hostSiteName property which is set in initializeScreenObject() method. The view is rendered properly with <td>${screenObject.getHostSiteName()}</td>. Now I click Cancel submit control. When controller takes over the submit, this property is null.

Please explain if this is expected or not. If expected, please explain how to go about it. I figured, I could add hidden form fields for those properties that need to survive the post but it's a bit of a hack. I hope there are better answers. And how does original request parameter come into focus within the post operation..?

Upvotes: 0

Views: 830

Answers (1)

sdouglass
sdouglass

Reputation: 2390

It sounds like expected behavior. The HostSiteSectionHeaderEditScreenObject instance passed as an argument to the POST handler method is not the same instance as the one you put in the model in the GET handler method. By default it's a new instance that Spring creates. Spring will bind values to the object's fields based on what parameters are present in the request for the POST. So if a parameter isn't present in the POST (e.g. because you didn't put an input in the HTML form for it) that field will not be set by Spring, it will just be whatever the default initial value is for the field.

It sounds like maybe what you want is to have initializeScreenObject() applied to your screen object then have the request parameter values applied after that? There are a couple ways to go about that. One would be to have a controller method annotated with @ModelAttribute:

@ModelAttribute("screenObject")
public HostSiteSectionHeaderEditScreenObject initScreenObject(@RequestParam(value="hostSiteId", required=false) Integer hostSiteId, @RequestParam(value="hostSiteSectionId", required=false) Integer hostSiteSectionId, Locale locale) {
    HostSiteSectionHeaderEditScreenObject screenObject=new HostSiteSectionHeaderEditScreenObject();

    initializeScreenObject(hostSiteId, hostSiteSectionId, screenObject, locale, true);
}

http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

If you do that, your GET handler method could be simplified to just return a view name.

Upvotes: 1

Related Questions