java Spring SimpleFormController onSubmit 请求参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2130683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring SimpleFormController onSubmit request parameters
提问by RedYeti
I am using SimpleFormControllerin 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.
我SimpleFormController在我的应用程序中使用来处理表单提交。我缺少的一件事是传递onSubmit(request,response..)的请求对象与formBackingObject(..).probably收到的初始请求对象不同,因为它又是一个来自网络的新请求。
i just want to use the same parameters from request object in onSubmit(..)that i was able to access in formBackingObject(..).
我只想使用来自请求对象的相同参数,onSubmit(..)因为我可以在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.
我可以将它们存储在其中并通过 jsp 中的隐藏字段传递,但我正在尝试获得一些优雅的方法。
is there any way to achieve this?
有没有办法实现这一目标?
EDIT:
编辑:
i am overriding
我是压倒性的
formBackingObject(HttpServletRequest request)`
and
和
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(..).
因为初始视图formbackingObject(..)将被调用,我将从请求对象中获得一些变量,然后如果用户提交表单onSubmit(..)将被调用,那么我将拥有另一个请求对象,该对象与我收到的请求对象不同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 ?'
我要问的是,有没有办法保存初始的“请求”参数(request.getParameter()有点……),以便我可以使用它们而
onSubmit(..)无需通过隐藏字段来回发送它们?
回答by Shane Bell
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.
formBackingObject()当用户请求初始表单视图时,将调用该方法。这是一个完全独立的 HTTP 请求,当用户提交表单时,onSubmit()即调用该方法时。
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.
如果您想保存第一个 HTTP 请求中的状态以便它在第二个 HTTP 请求中可用,您最好的选择可能是将其保存在 HTTP 会话中。
eg: in your formBackingObject()method:
例如:在你的formBackingObject()方法中:
HttpSession session = request.getSession();
session.setAttribute("param1", request.getParameter("param1"));
session.setAttribute("param2", request.getParameter("param2"));
and in your onSubmit()method:
并在您的onSubmit()方法中:
HttpSession session = request.getSession();
String param1 = (String) session.getAttribute("param1");
String param2 = (String) session.getAttribute("param2");
回答by RedYeti
formBackingObject()is expected to set up the "command" Object that is supplied to onSubmit()on the next submission. The command Object is just a POJOthat you can create and add attributes to as needed.
formBackingObject()预计将设置onSubmit()在下一次提交时提供的“命令”对象。命令 Object 只是一个POJO,您可以根据需要创建和添加属性。
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 cruftfloating around in your session.
通常,您使用一些数据填充命令对象,这些数据将通过服务调用(最终来自数据库)显示在屏幕上,但没有什么可以阻止您将到达请求中的数据formBackingObject()放入命令对象中并将其拉回再次onSubmit()调用时。这样做的好处是你不会在你的会话中留下 cruft。
For example, put a boolean from the request in MyCommandin formBackingObject():
例如,将请求中的布尔值放入MyCommandin 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():
然后在请求中找到那个布尔值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).
请注意, myCommand 仅对会话可见(因此其他登录用户无法看到它,并且在会话结束时将其销毁)。

