View Full Version : How exactly to use a Redirect View?
blueskies
Aug 8th, 2007, 04:31 PM
Currently I have a Form Controller which when it returns a ModelAndView in the onSubmit method it doing a JstlView. However, when I hit the back button on the browser it always prompts me with a message about Post Data. Is there a way to get around this by using RedirectView?
How can I define a RedirectView in my xml config file? Do I have to map it to a URL which than maps to a Controller, like this?
<bean id="urlmap" ... >
<props>
<prop key="afterForm.html">afterFormController</prop>
...
</props>
</bean>
....
<bean id="afterFormControl" class="...">
...
</bean>
<bean id="afterFormView" ..>
<property name="url">afterForm.html</property>
</bean>
Based on the form entry, I also have to pass that information along to the next view, so how can I do that exactly?
sami25
Aug 8th, 2007, 11:37 PM
Hi,
one of the ways of using redirect views is
define a bean in the spring configuration as follows
<bean id="RedirectController" class="org.springframework.web.servlet.mvc.Parameterizabl eViewController">
<property name="viewName"><value>redirectsuccess</value></property>
</bean>
define a mapping for the bean if you are using the SimpleUrlMapping as follows
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/success.do">RedirectController</prop>
</props>
</property>
<bean id="myMultiActionController" class="myowncustomclassthatextendsmultiactioncontroller">
<property name="successView" value="success"/>
In the views.properties use the following.
success.class=org.springframework.web.servlet.view .RedirectView
success.url=/success.do
success.contextRelative=true
redirectsuccess.url=/WEB-INF/jsp/redirectsuccess.jsp
gr8riaz
Aug 9th, 2007, 02:02 AM
There is another way to use the redirect view. when you redirect in the Form Controller use following:
return new ModelAndView(new RedirectView(request.getContextPath()+ "redirectlink.do"), parameterMap);
When you redirect by using the above code it will not resubmit the page when you press browsers back or refresh button. :)
blueskies
Aug 9th, 2007, 10:45 AM
Hi,
one of the ways of using redirect views is
define a bean in the spring configuration as follows
<bean id="RedirectController" class="org.springframework.web.servlet.mvc.Parameterizabl eViewController">
<property name="viewName"><value>redirectsuccess</value></property>
</bean>
define a mapping for the bean if you are using the SimpleUrlMapping as follows
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlH andlerMapping">
<property name="mappings">
<props>
<prop key="/success.do">RedirectController</prop>
</props>
</property>
<bean id="myMultiActionController" class="myowncustomclassthatextendsmultiactioncontroller">
<property name="successView" value="success"/>
In the views.properties use the following.
success.class=org.springframework.web.servlet.view .RedirectView
success.url=/success.do
success.contextRelative=true
redirectsuccess.url=/WEB-INF/jsp/redirectsuccess.jsp
Why does the class have to extend MultiActionController?
I don't really understand the views.properties file.
pmularien
Aug 9th, 2007, 10:52 AM
Why does the class have to extend MultiActionController?
I don't really understand the views.properties file.
He/she was just providing an example - I would look at what "gr8riaz" posted for a simpler example - basically in the ModelAndView returned from your controller, return a RedirectView instead of a standard view reference. You may also use the special "redirect:" prefix on your view name. The key to remember with redirect view is that you'll need to include whatever suffix you're using in your URL mapping (for example, .do, .html, etc). This will obviously differ based on your configuration of Spring MVC.
The use of redirect views is covered in pretty good detail in section 13.5.3 (http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html#mvc-redirecting) of the Spring manual.
blueskies
Aug 9th, 2007, 10:56 AM
He/she was just providing an example - I would look at what "gr8riaz" posted for a simpler example - basically in the ModelAndView returned from your controller, return a RedirectView instead of a standard view reference. You may also use the special "redirect:" prefix on your view name. The key to remember with redirect view is that you'll need to include whatever suffix you're using in your URL mapping (for example, .do, .html, etc). This will obviously differ based on your configuration of Spring MVC.
The use of redirect views is covered in pretty good detail in section 13.5.3 (http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html#mvc-redirecting) of the Spring manual.
Thank you. I'm not sure if this would work ? How would I pass objects along? For instance, user enters information on a form then a query is done on the database and results are shown. Can I pass this to a redirect view?
pmularien
Aug 9th, 2007, 11:07 AM
Thank you. I'm not sure if this would work ? How would I pass objects along? For instance, user enters information on a form then a query is done on the database and results are shown. Can I pass this to a redirect view?
No, redirect views are implemented by a 302 or 303 redirect, so redirecting of "data" only happens by putting name/value pairs from the Model onto the redirect URL. Since full objects can't be serialized into String form (the toString() method of each object in the Model is called), this won't work as you describe. A way to work around this is to store the search results in the user session, or to pass some sort of externalizable reference to the search results data (e.g. GUID or primary key) that would let you look up the results in the receiving controller. Hopefully this makes sense?
Generally, you don't use redirects for search / search results type patterns in Spring - these are usually handled by the same controller (search results being returned in the ModelAndView sent back from your onSubmit handler).
blueskies
Aug 9th, 2007, 11:09 AM
No, redirect views are implemented by a 302 or 303 redirect, so redirecting of "data" only happens by putting name/value pairs from the Model onto the redirect URL. Since full objects can't be serialized into String form (the toString() method of each object in the Model is called), this won't work as you describe. A way to work around this is to store the search results in the user session, or to pass some sort of externalizable reference to the search results data (e.g. GUID or primary key) that would let you look up the results in the receiving controller. Hopefully this makes sense?
Generally, you don't use redirects for search / search results type patterns in Spring - these are usually handled by the same controller (search results being returned in the ModelAndView sent back from your onSubmit handler).
Thanks, maybe in my case it is better to handle it by the same controller. Is a redirect view better in cases where it's going to a static page (such as "Thank for your vote") - something that isn't going to change?
pmularien
Aug 9th, 2007, 11:16 AM
Thanks, maybe in my case it is better to handle it by the same controller. Is a redirect view better in cases where it's going to a static page (such as "Thank for your vote") - something that isn't going to change?
Correct - the other common pattern is after edit of an object, to redirect back to somewhere (either view or edit page), passing along the object primary key in the RedirectView model. The primary reason for using a redirect after submit is to avoid inadvertent duplicate submits if the user reloads the page.
blueskies
Aug 9th, 2007, 12:06 PM
Correct - the other common pattern is after edit of an object, to redirect back to somewhere (either view or edit page), passing along the object primary key in the RedirectView model. The primary reason for using a redirect after submit is to avoid inadvertent duplicate submits if the user reloads the page.
After looking at this further I might have to use a Redirect view... ?
I have search results come back, but only 10 are showed at a time with links at the top reflecting a page. So, if they click #2 link it will show results 11-20. I may need another Controller to handle all this, right? I don't think it can be done in the same Form Controller
pmularien
Aug 9th, 2007, 12:17 PM
After looking at this further I might have to use a Redirect view... ?
I have search results come back, but only 10 are showed at a time with links at the top reflecting a page. So, if they click #2 link it will show results 11-20. I may need another Controller to handle all this, right? I don't think it can be done in the same Form Controller
You could do it either way. Where are you planning on storing the search results? In a database? In the session?
blueskies
Aug 9th, 2007, 12:23 PM
You could do it either way. Where are you planning on storing the search results? In a database? In the session?
In the session. Is there a pro/con of this? I figured many users can come searching for many things, saving into a database would just be too much effort. It doesn't have to be saved outside the time they search.
pmularien
Aug 9th, 2007, 12:30 PM
In the session. Is there a pro/con of this? I figured many users can come searching for many things, saving into a database would just be too much effort. It doesn't have to be saved outside the time they search.
And what are you using for the formBackingObject? A value object representing your search criteria? The search results? Or a combination of both?
blueskies
Aug 9th, 2007, 01:34 PM
And what are you using for the formBackingObject? A value object representing your search criteria? The search results? Or a combination of both?
The form backing object is a bean which contains the user input fields.
pmularien
Aug 9th, 2007, 02:02 PM
The form backing object is a bean which contains the user input fields.
OK, thanks for the Q&A - it helps me give you a better answer :)
If the search form and search results are shown on the same page, then yes, I would use the same controller.
If they are not, then I wouldn't use the same controller. If this is the case, then the second controller (associated with the "display results from session" action) needn't be a *FormController subclass, and instead could be a *CommandController subclass. Your pagination "action" will need to be recognized by your controller, and you would set model data for display on the page based on the combination of (1) the results stored in the session, (2) the URL parameters responsible for figuring out what subset of the results to display (usually these are 'start at' and 'end at' or something similar).
If you ARE using the same controller (as mentioned in the first paragraph), it will be a little trickier, only because the "next page" action is most likely not going to be a form POST, but instead would be represented by an a href (GET). In this case, you'll have to be a little creative (hint: look at overriding isFormSubmission) and handle the different possible actions on the page (search, next page, previous page) in your onSubmit method.
Hopefully this helps?
blueskies
Aug 9th, 2007, 02:22 PM
OK, thanks for the Q&A - it helps me give you a better answer :)
If the search form and search results are shown on the same page, then yes, I would use the same controller.
If they are not, then I wouldn't use the same controller. If this is the case, then the second controller (associated with the "display results from session" action) needn't be a *FormController subclass, and instead could be a *CommandController subclass. Your pagination "action" will need to be recognized by your controller, and you would set model data for display on the page based on the combination of (1) the results stored in the session, (2) the URL parameters responsible for figuring out what subset of the results to display (usually these are 'start at' and 'end at' or something similar).
If you ARE using the same controller (as mentioned in the first paragraph), it will be a little trickier, only because the "next page" action is most likely not going to be a form POST, but instead would be represented by an a href (GET). In this case, you'll have to be a little creative (hint: look at overriding isFormSubmission) and handle the different possible actions on the page (search, next page, previous page) in your onSubmit method.
Hopefully this helps?
I was going to leave them in a separate page. I figured it would be easier that way. :)
The user input would be stored in a session Object. The page would be a GET (so URL ?page=2). I know in my Controller I have to have some logic to handle the pages.
I have not gotten too far yet, but one thing I thought about that might be an issue is how long are objects saved into sessions? I wouldn't want the user to click page 6 and then the session object is gone. Hopefully that won't be an issue.
Blair
Aug 9th, 2007, 11:43 PM
Maybe the display tag library is what you are after.
or just making the Controller you are currently using a bit smarter.
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.