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“.
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");
}
}
One of the use case is applying the “RedirectView” concept to solve the duplicated form submission in Spring MVC.
What if I have a POST request ?
And I have to send it to another REST Server ?
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));
}
}
Hi,
In my program, I am redirecting request to another server by using RedirectView. It is working. But I want to know in the program once the request is processed completely. Can any one tell how can I do it?
Superb explanation . Also please include this point that while adding redirect prefix in controller , we can comment spring-views.xml file and no need of XmlViewresolver in spring beans file .
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.
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??
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 !!!
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 !
Thanks for your knowledge
Thanks, solved a problem to me!
Error coming when i tried to run this example
Line 5 in XML document from resource [/WEB-INF/mvc-dispatcher-servlet.xml] is invalid.
Please download the example and build with Maven.
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
Sorry, do not really get your question, mind to zip it and send to me for debugging? (exclude dependency jar)
hi mkyong,
keep up the good work
Your website is very useful
i learnt so many new things
thanks very much
regards
Ravi