简体   繁体   English

使用获取请求的Spring MVC绑定命令对象

[英]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. 问题在于,我被要求不使用POST请求而仅使用GET请求来执行此操作,然后再失去默认的数据绑定功能,这对我们来说是一件很愉快的事情。

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). 但是,尽管请求对象中存在值,但getCommand返回了一个没有值的全新CommandObject(我可以使用HttpServletRequest的getParameter方法进行检索)。 But there isn't any binding. 但是没有任何约束力。

So the question : 所以问题是:

1) Is there any way to archive this? 1)有什么办法可以存档吗?

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. 2)同样非常重要的是,表格中的所有值都将丢失,并且最终(如果解决了此问题)我将需要为用户“保留”过滤器,以避免在第一次搜索后重新输入。

  • Auto Response : setSessionForm(true); 自动响应:setSessionForm(true); looks like can do the work! 看起来可以做的工作! (According to javadoc) (根据javadoc)

Thanks to all! 谢谢大家!

Greetings 问候

Victor. 胜利者。

Okey, i found a way to archive what a was looking for. Okey,我找到了一种存档所需内容的方法。

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). 我将为那些以前遇到过相同问题的人进行解释,并希望找到有经验的用户来验证此方法...一些安静的共同点是,有多种方法可以做相同的事情,而人类很难知道没有正确地确认正确的路径..因此,我发现它在AbstractFormController内部查找(这在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). 但是使用setSessionForm(true)还不够。 According to javadoc the setBindOnNewForm(boolean) method does the following : 根据javadoc,setBindOnNewForm(boolean)方法执行以下操作:

/**
 * 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 : 因此,我的猜测是必须将这两个标志标记为true,因为:

  • 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). setSessionForm使得可以将表单对象存储为会话属性,因此“存储在会话中以在请求之间保留表单对象实例,而不是在每个请求上创建一个新的对象”(根据setSessionForm方法的javadoc)。
  • The setBindOnNewForm allows the population of the form object with the initial request (despites what type of request method we have). setBindOnNewForm允许使用初始请求填充表单对象(描述我们拥有的请求方法的类型)。 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..." 根据javadoc发现AbstractFormController “仅当bindOnNewForm设置为true时,才会应用ServletRequestDataBinder来使用初始请求参数填充新的表单对象...”

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). 但是,我仍然注意到,在控制器流中使用调试器后,填充正在方法“ getErrorsForNewForm(HttpServletRequest request)”中发生。. 如果 setBindOnNewForm为true,则在其中使用ServletRequestDataBinder类型的具体对象,以后(如javadoc所述),将调用onBindOnNewForm方法,从而允许程序员使用自定义行为覆盖它,默认行为只是空的(再次针对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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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