开发者

Spring SimpleFormController onSubmit request parameters

I am using SimpleFormController in my application to handle form submisions. one thing i am missing is the request object that is passed onSubmit(request,response..) is a different one from the initial request object that is received by formBackingObject(..).probably because it is again a new request from web.

i just want to use the same parameters from request object in onSubmit(..) that i was able to access in formBackingObject(..).

it could be possible for me to store them in and pass thru hidden fields from jsp, but i am trying to get some elegant approach.

is there any way to achieve this?

EDIT:

i am overriding

formBackingObject(HttpServletRequest request)`  

and

开发者_StackOverflow社区
onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)

methods in my class.

for the intial view formbackingObject(..) will be called and i will have some variables from the request object then if user submits the form onSubmit(..) will be called then i will have another request object which is different from the one i received in formbackingObject(..) .

what i am asking is, is there any way of holding the initial 'request' parameters(request.getParameter() kind of...) so that i can use them in onSubmit(..) with out sending them back & forth thru hidden fields ?'


The formBackingObject() method is being called when the user makes the request for the initial form view. This is a completely separate HTTP request to when the user submits the form which is when the onSubmit() method is called.

If you want to save state from the first HTTP request so it is available in the second HTTP request, your best option is probably to save it in the HTTP session.

eg: in your formBackingObject() method:

HttpSession session = request.getSession();
session.setAttribute("param1", request.getParameter("param1"));
session.setAttribute("param2", request.getParameter("param2"));

and in your onSubmit() method:

HttpSession session = request.getSession();
String param1 = (String) session.getAttribute("param1");
String param2 = (String) session.getAttribute("param2");


formBackingObject() is expected to set up the "command" Object that is supplied to onSubmit() on the next submission. The command Object is just a POJO that you can create and add attributes to as needed.

Normally you populate the command Object with some data to be displayed on the screen from a service call (which ultimately comes from the DB) but there's nothing to stop you placing data that arrived in the request in formBackingObject() into the command Object and pulling it back off again when onSubmit() is called. The advantage of doing this is that you don't end up with cruft floating around in your session.

For example, put a boolean from the request in MyCommand in formBackingObject():

    protected Object formBackingObject(HttpServletRequest request)
    {
        boolean someBoolean = ServletRequestUtils.getBooleanParameter(request, "someBoolean");
        MyCommand myCommand = new MyCommand();
        myCommand.setSomeBoolean(someBoolean);
        // optionally get some data from the DB to set on myCommand here...
        return myCommand;
    }

Then find that boolean inside the request in onSubmit():

    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors)
    {
        MyCommand myCommand = (MyCommand) command;
        if (myCommand.getSomeBoolean()) {
            System.out.println("someBoolean was set in formBackingObject!");
        }
    }

Note that myCommand is only visible to the session (so no other logged in users can see it, and it is destroyed when the session ends).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜