添加参数后向前推进?
有没有办法将请求转发到另一个控制器,同时向其添加一些参数数据?我尝试添加到 ModelMap,但它似乎没有出现。我正在做类似的事情:
return "forward:/my-other-controller";
我能想到的唯一其他方法是将参数放在会话上,然后将它们弹出到目标控制器中。
Is there a way to forward a request to another Controller while adding some parameter data to it? I tried adding to the ModelMap, but it doesn't seem to hang around. I am doing something like:
return "forward:/my-other-controller";
Only other way I can think of is to put the parameters on the session and then pop them off in the target controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是将数据添加到请求中。由于这是转发,因此相同的请求会传递到服务器内的不同处理程序。
例如,让我们从两个控制器的简单设置开始,一个转发到另一个:
添加数据的第一种方法是将其设置为请求的属性。新的控制器将如下所示(A):
由于 转发前缀基本上是一个URL,你还可以有以下版本(属性更改为参数)(B ):
还可以进一步简化第二个控制器通过使用注释来代替:
只是为了它的乐趣,并展示Spring的实际绑定行为,你甚至可以这样做(C):
我个人会选择解决方案A对于许多参数,解决方案 B 对于一些参数。 解决方案 C 有一种“嗯...?!”的感觉。效果,所以我会避免它(它也适用于添加到 URL 的参数,所以其中一些参数或者你会得到一个混乱的 URL)。
在会话中添加数据当然也会起作用,但会不必要地延长数据的生命周期,因此最好的位置是在转换到第二个控制器期间将其添加到请求中。
The simplest way is to add the data to the request. Since this is a forward, the same request is passed around to different handlers within the server.
As example, let's start with a simple setup of two controllers, one forwarding to the other:
First way to add the data is to set it as attributes on the request. The new controllers will look like this (A):
Since the view name in the forward prefix is basically an URL, you can also have the following versions (attribute changed to parameter) (B):
You can also further simplify the second controller by using annotations instead:
And just for the fun of it, and to show Spring's binding behavior in action, you could do it even like this (C):
I would personally go with solution A for many parameters, and solution B for a few. Solution C has a sort of "huh...?!" effect so I would avoid it (also it works with parameters added to the URL so a few of those or you get a messy URL).
Adding the data in the session would also work off course, but would extend the data's life time unnecessarily, so the best place is to add it on the request during the transition to the second controller.
考虑转发就像重定向一样,但在这个意义上不需要上下文和主机(转发由服务器内部处理,而重定向指示用户的浏览器请求不同的 url)。
所以,是的,使用会话、cookie 或发布或获取参数。
Consider forward just like redirect but without the need for a context and host in this sense (forward is handled internally by the server, whereas redirect instructs the user's browser to request a different url).
So, yes, use the session, cookies or post or get parameters.
你考虑过过滤器吗?您可以使用委托过滤器代理创建一个过滤器,这将使您能够访问请求参数以及 spring beans。
Have you considered filters? You could create a filter using the delegating filter proxy which will gives you access to the request parameters as well as the spring beans .
我发现 这篇文章,使用
HttpRequestWrapper
:第一:
定义
HttpRequestWrapper
并重写方法getParameter
、getParameterMap
、getParameterNames
和getParameterValues
,如下所示在帖子中。第二:
通过 RequestDispatcher 转发到您的网址,使用方法参数中的包装请求:
我已经尝试过删除参数(这甚至更困难!)并且它有效!
I've found this post, that uses an
HttpRequestWrapper
:First:
Define the
HttpRequestWrapper
and override the methodsgetParameter
,getParameterMap
,getParameterNames
andgetParameterValues
like in the post.Second:
Forward via RequestDispatcher to your url, using the wrapped request in the method parameters:
I've tried this out for deleting of parameters (which is even more difficult!) and it works!