headz68
headz68

Reputation: 197

Spring MVC 2.5 - How to add parameters to a redirect that don't show in the url?

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?

Upvotes: 1

Views: 6461

Answers (2)

DwB
DwB

Reputation: 38338

Have you considered just calling the second handler from your current handler? Something like this:

public ModelAndView currentHandler(...)
{
  ModelAndView returnValue;

  ... stuff here...
  if (make a decision)
  {
    returnValue = ... set the model and view as normal.
  }
  else
  {
    returnValue = ClassName.someOtherHandler( ... parameters ... );
  }

  return returnValue;
}

Upvotes: 0

Ryan Stewart
Ryan Stewart

Reputation: 128939

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.

Upvotes: 1

Related Questions