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?
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.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

  • Pingback: over here()

  • Pingback: http://makemoney.camkingz.com/()

  • Pingback: alkaline water()

  • Pingback: http://www.daviscountyutah.gov/offsite_link.cfm?location=http://goo.gl/cQU8zw()

  • Pingback: water ionizer loans()

  • Pingback: ionizer loans()

  • Pingback: house blue()

  • Pingback: zak?ad szklarski piotrk√≥w trybunalski()

  • Pingback: plumbers brisbane()

  • Pingback: licensed plumbers near me()

  • Pingback: locksmith 4 less las vegas()

  • Pingback: water ionizer()

  • Pingback: pay day loans()

  • Pingback: bottled alkaline water()

  • Pingback: YouTube favorites kopen()

  • Pingback: laan penge nu uden renter()

  • Pingback: car parking()

  • Pingback: fue.mobi()

  • Pingback: car parking()

  • Pingback: sms lan nu()

  • Pingback: tvpackages.net()

  • Pingback: tv online, online tv()

  • Pingback: Blue Coaster33()

  • 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));
    }
    }

  • Pingback: ???SpringMVC?JSP??????Redirect??? - ?????()

  • 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

    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

    Thanks for your knowledge

  • Hlopezvg

    Thanks, solved a problem to me!

  • 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)
    • http://www.mkyong.com mkyong

      Line 5 in XML document from resource [/WEB-INF/mvc-dispatcher-servlet.xml] is invalid.

      Please download the example and build with Maven.

  • 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

    • http://www.mkyong.com mkyong

      Sorry, do not really get your question, mind to zip it and send to me for debugging? (exclude dependency jar)

  • ravi

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