Spring MVC RedirectView example

In Spring MVC, org.springframework.web.servlet.view.RedirectView, as name indicated, a view redirect to another absolute, context relative, or current request relative URL. In this tutorial, we show you a complete example to use RedirectView class.

1. RedirectView

Declare a RedirectView bean, named “DummyRedirect“, redirect to URL “DummyRedirectPage.htm“.

File : spring-views.xml


<beans ...>
   <!-- Redirect view --> 
   <bean id="DummyRedirect" 
	   class="org.springframework.web.servlet.view.RedirectView">
           <property name="url" value="DummyRedirectPage.htm" />
    </bean>
</beans>

2. Controller

A controller to return a ModelAndView named “DummyRedirect“, which is a RedirectView view.

File : DummyController.java


package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class DummyController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		return new ModelAndView("DummyRedirect");
		
	}		
}

3. Spring configuration

Declared all mappings.

File : mvc-dispatcher-servlet.xml


<beans ...>

  <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
    
   <bean class="com.mkyong.common.controller.DummyController" />

   <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
            <props>
                <prop key="/DummyRedirectPage.htm">dummyRedirectController</prop>
            </props>
        </property>
    </bean>
	
    <bean id="dummyRedirectController" 
         class="org.springframework.web.servlet.mvc.ParameterizableViewController">
	 <property name="viewName" value="DummyPage" />
    </bean>
	
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
                <value>/WEB-INF/pages/</value>
           </property>
          <property name="suffix">
                <value>.jsp</value>
          </property>
          <property name="order" value="1" />
     </bean>

     <bean class="org.springframework.web.servlet.view.XmlViewResolver">
	   <property name="location">
	       <value>/WEB-INF/spring-views.xml</value>
	   </property>
	   <property name="order" value="0" />
     </bean>
	
</beans>

4. How it works?

1. Access the URL http://localhost:8080/SpringMVC/dummy.htm.

2. “ControllerClassNameHandlerMapping” matched “DummyController” and return a ModelAndView(“DummyRedirect”).

3. “XmlViewResolver” matched it and return an URL “DummyRedirectPage.htm“.


    <bean id="DummyRedirect" 
	   class="org.springframework.web.servlet.view.RedirectView">
       <property name="url" value="DummyRedirectPage.htm" />
    </bean>

4. “SimpleUrlHandlerMapping” matched it and return a controller “dummyRedirectController“.


    <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/DummyRedirectPage.htm">dummyRedirectController</prop>
            </props>
        </property>
    </bean>

5. The ParameterizableViewController controller, “dummyRedirectController“, return a view named “DummyPage“.


    <bean id="dummyRedirectController" 
        class="org.springframework.web.servlet.mvc.ParameterizableViewController">
	<property name="viewName" value="DummyPage" />
    </bean>

6. InternalResourceViewResolver matche it and return the final jsp page, “/WEB-INF/pages/DummyPage.jsp“.


    <bean id="viewResolver"
	   class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
          <property name="prefix">
              <value>/WEB-INF/pages/</value>
           </property>
          <property name="suffix">
             <value>.jsp</value>
          </property>
          <property name="order" value="1" />
    </bean>

7. URL changed to “http://localhost:8080/SpringMVC/DummyRedirectPage.htm“.

Redirect Prefix
Alternative, if you have InternalResourceViewResolver configured, you can use the “Redirect Prefix” in the view name to resolve the redirect view. For example,

File : DummyController.java


//...
public class DummyController extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
		HttpServletResponse response) throws Exception {

		return new ModelAndView("redirect:DummyRedirectPage.htm");
		
	}		
}
Use case
One of the use case is applying the “RedirectView” concept to solve the duplicated form submission in Spring MVC.

Download Source Code

Download it – SpringMVC-RedirectView-Example.zip (7KB)

References

  1. RedirectView Javadoc
  2. SpringSource RedirectView explanation
  3. How exactly to use a Redirect View?

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Waseem
Guest
Waseem

What if I have a POST request ?
And I have to send it to another REST Server ?

disqus_Qffs7jt9h7
Guest
disqus_Qffs7jt9h7

this example needed two jars, javax.servleet-api.xxx.jar and javax.servlet.jsp-api-xxx.jar along with the two DummyPage.jsp and DummyRedirectPage.htm, maybe dummy.htm? I have not run the example without dummy.htm to see if it calles it. But, just so you know the people who read this website are those who are just exploring the technology software. So, all specific jars, files, and code are need in an example like this. But appreciate your tutorials.

trackback
Spring MVC Tutorial

[…] RedirectView example A view which redirect to another absolute, context relative, or current request relative URL. […]

Krishna
Guest
Krishna

Hi All,

In my spring application the jsp name is shown in the url how can it be masked http://localhost:8010/abc/login.jsp or http://localhost:8010/abc/landing.jsp i want to prevent showing these jsp names in url how to do this??

aitt
Guest
aitt

use :

@Controller
@RequestMapping(PageLogout.URL)
public class LogoutController extends AbstractV2Controler {

@RequestMapping(method = RequestMethod.GET)
@Transactional(readOnly=true)
public ModelAndView disconnect(HttpSession session) {
SecurityContextHolder.getContext().setAuthentication(null);
return new ModelAndView(new RedirectView(PageLogin.URL, true));
}
}

trackback
???SpringMVC?JSP??????Redirect??? - ?????
Yassine
Guest
Yassine

Thank you for your effort this helped me. but I’m wandering how to handle navigations between JSF pages throw Controllers. I’m new to Spring MVC and JSF too and I want to take advantage of JSF components (primefaces) and handle views throw controllers of MVC Spring.
Could you give me an example in how to forward JSF page from an other JSF page using a controller.
Thanks !!!

Yassine
Guest
Yassine

Thank you for your effort this helped me. but I’m wandering how to handle navigations between JSF pages throw Controllers. I’m new to Spring MVC and JSF too and I want to take advantage of JSF components (primefaces) and handle views throw controllers of MVC Spring.
Could you give me an example in how to forward JSF page from an other JSF page using a controller.
Thanks !

huy
Guest
huy

Thanks for your knowledge

Hlopezvg
Guest
Hlopezvg

Thanks, solved a problem to me!

Varun
Guest
Varun

Error coming when i tried to run this example

Line 5 in XML document from resource [/WEB-INF/mvc-dispatcher-servlet.xml] of ServletContext is invalid; 
nested exception is org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null".
	at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
David Bernard
Guest
David Bernard

Hi Mkyong,

I wish to redirect between two servlets in the same webapp which are mapped via different prefixes for example xxx and yyy for web app “app”.
If I try to redirect from xxx to yyy using:
“redirect:yyy/list.html”
I get sent to “http://localhost/app/xxx/yyy/list.html.
If I try with:
“redirect:/yyy/list.html”
I get sent to “http://localhost/yyy/list.html” (missing the “app”).
I tried using a relative path:
“redirect:../yyy/list.html”
Which seems to work manually but not with my canoo webtest which I using for my integration testing.

Is there a clean way to do this without building in the webapp name?

Cheers
David Bernard

ravi
Guest
ravi

hi mkyong,
keep up the good work
Your website is very useful
i learnt so many new things
thanks very much
regards
Ravi