简体   繁体   中英

Spring MVC Binding Command Object Using Get Request

I need to implement a controller that has a command object that is backing a filtering form for a search across multiple entries.

The problem is that the i was asked to do that without using POST request, instead using GET request only, and there before loosing the functionality of the default data binding that springs makes happily for us.

So i tried to implement a method, inside my controller, that looks like this:

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    if (isSearchRequest(request)) {
        MyCommandObject myCommandObject = (MyCommandObject) getCommand(request);
        System.out.println(managePositionsForm);
    }
    return super.handleRequestInternal(request, response);
}

But the getCommand returns me a brand new CommandObject with no values, despite that the values are present in the request object (i could retrieve then using the getParameter method of HttpServletRequest). But there isn't any binding.

So the question :

1) Is there any way to archive this?

2) Also is very important, that all the values in the form, are lost and, eventually (if this problem is solved) i will need to "persist" the filters for the users in order to avoid re entering after the first search.

  • Auto Response : setSessionForm(true); looks like can do the work! (According to javadoc)

Thanks to all!

Greetings

Victor.

Okey, i found a way to archive what a was looking for.

I will explain for the sake of those have the same problem before, and hoping to find a experienced user to validate this method... some quiet common is there a multiple ways to do a same thing and as human beings is very difficult to know without proper acknowledge the right path.. so this ia found looking inside the AbstractFormController (that is excellently documented with javadoc).

So what i did was the following, on my controller constructor i add these lines at the end :

    setSessionForm(true);
    setBindOnNewForm(true);

That all the magic!

But is not enought with setSessionForm(true). According to javadoc the setBindOnNewForm(boolean) method does the following :

/**
 * Set if request parameters should be bound to the form object
 * in case of a non-submitting request, i.e. a new form.
 */

So my guess are that these two flags are necessary to be marked as true, because :

  • The setSessionForm makes posible to store as a session attribute the form object, so "is stored in the session to keep the form object instance between requests, instead of creating a new one on each request" (according to javadoc of the setSessionForm method).
  • The setBindOnNewForm allows the population of the form object with the initial request (despites what type of request method we have). According the javadoc found the AbstractFormController "Only if bindOnNewForm is set to true, then ServletRequestDataBinder gets applied to populate the new form object with initial request parameters..."

But still i noticed, following the controller flow with a debugger, that the population is happening inside the method "getErrorsForNewForm(HttpServletRequest request)".. that is where a concrete object of type ServletRequestDataBinder is used IF the setBindOnNewForm is true, and later (as the javadoc stated) the onBindOnNewForm method is invoked, allowing the programmer to overwrite it with custom behavior, the default behavior is just empty (again this was double checked against the code of AbstractFormController).

I have an strong felling to validate my thoughts here, so if anyone can help me, that would be alright, besides the problem is solved!

Thanks to all in advance!

Greetings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM