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 :
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 making a donation to this charity, thanks.

Related Posts

Popular Posts