Spring MVC 2.5 - 如何向网址中未显示的重定向添加参数?
为了简单起见,我的应用程序的基本功能是一个带有搜索结果表单的搜索界面。该应用程序通过 SOAP 从另一个应用程序获取这些结果。
在搜索控制器上,我需要能够在重定向中指定一些不会显示在结果页面的 URL 中的参数以及其他会显示的参数。下面是重定向的示例:
return new ModelAndView(new RedirectView(/results?q=blah, true));
如您所见,我需要包含 q 参数。但我还需要其他我不想显示在结果页面网址上的参数。
我的应用程序需要能够在一个浏览器会话中进行多次搜索。例如,多个选项卡同时进行搜索。因此,会话对象被抽象为包含会话状态对象的映射。它们保存每次搜索的状态数据。每次向搜索控制器发出请求时都会创建一个新的会话状态。然后结果控制器获取该会话状态。
我们的书签过程需要完全无状态。因此,人们应该能够为结果页面 URL 添加书签,或者将 URL 复制并粘贴到新选项卡或其他浏览器中。当执行结果 url 时,结果控制器将执行新的搜索。
我的问题是这样的: 我需要一种方法让结果控制器知道请求是来自搜索控制器中的重定向还是来自(书签/复制和粘贴)。这样它就可以获取现有的会话状态数据或执行新的搜索。如果我可以在搜索控制器的重定向中传递隐藏参数(例如“请求者”),那么我可以在结果控制器中使用简单的逻辑来查找现有会话状态或触发新搜索。
谢谢
编辑:如何将数据从一个控制器传递到另一个控制器而不将其放入 url 中。例如访问从搜索控制器重定向到结果控制器而不是结果视图中传递的模型数据?这是一个带有隐藏模型属性选项的构造函数。
/**
* Create a new RedirectView with the given URL.
* @param url the URL to redirect to
* @param contextRelative whether to interpret the given URL as
* relative to the current ServletContext
* @param http10Compatible whether to stay compatible with HTTP 1.0 clients
* @param exposeModelAttributes whether or not model attributes should be
* exposed as query parameters
*/
public RedirectView(String url, boolean contextRelative, boolean http10Compatible, boolean exposeModelAttributes)
是否有另一种方法将数据从一个控制器传递到另一个控制器?
To keep it simple, the basic functions of my application are a search interface with a form for searching results. The app fetches these results via SOAP from another app.
On the search controller I need the ability to specify some parameters in the redirect that won't show up in the url of the results page and others that will. Here's an example of the what the redirect looks like:
return new ModelAndView(new RedirectView(/results?q=blah, true));
As you can see I need the q param to be included. But I also need other parameters that I don't want to show up on the results page url.
My app needs the ability to have multiple searches within one browser session. For example, having multiple tabs all searching at the same time. So the session object is abstracted to contain a map of session state objects. These hold state data for each search. A new session state is created each time a request is made to the search controller. This session state is then fetched by the results controller.
Our bookmarking process needs to be completely state free. So a person should be able to bookmark the results page url or copy and paste the url into a new tab or another browser. When the results url is executed, the results controller executes a new search.
My problem is this:
I need a way for the results controller to know whether the request is coming from a redirect in the search controller or from a (bookmark/copy & paste). That way it can grab the existing session state data or execute a new search. If I can pass a hidden parameter in the redirect from the search controller such as "requester", then I can use simple logic in the results controller to look for an existing session state or fire a new search.
Thanks
EDIT: What about passing data from one controller to the other without putting it in the url. Such as accessing model data passed from the search controller redirect in the results controller instead of the results view? Here's a constructor with the option to hide the model attributes.
/**
* Create a new RedirectView with the given URL.
* @param url the URL to redirect to
* @param contextRelative whether to interpret the given URL as
* relative to the current ServletContext
* @param http10Compatible whether to stay compatible with HTTP 1.0 clients
* @param exposeModelAttributes whether or not model attributes should be
* exposed as query parameters
*/
public RedirectView(String url, boolean contextRelative, boolean http10Compatible, boolean exposeModelAttributes)
Is there another way to pass data from one controller to the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTTP 重定向响应,通常由 301 响应代码,在其 Location 标头中包含客户端应请求的 URI,以便查找指定的资源。无法告诉客户端有关要发出的请求的任何其他信息,只能告诉客户端它应该请求的 URI。因此,只有可以进入 URI 的内容才能通过重定向发回。您可以根据 Referer 标头进行一些猜测,但这并不能保证。
An HTTP redirect response, normally designated by a 301 response code, contains a URI in its Location header that a client should request in order to find the specified resource. There's no way to tell the client anything else about the request to be made, only the URI it should request. Therefore only things that can go in the URI can be sent back in a redirect. You could make some guesses based on the Referer header, but that's no guarantee.
您是否考虑过从当前处理程序中调用第二个处理程序?像这样的东西:
Have you considered just calling the second handler from your current handler? Something like this: