JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Spring MVC Redirect Example

May 9, 2014 by Krishna Srinivasan Leave a Comment

This tutorial explains how to redirect to a new URL inside the Spring MVC controller. There is attribute “redirect” which can be prefixed to the view name to indicate that the new request should be a redirected one.

also read:

  • Spring 4 Tutorials
  • Introduction to Spring MVC Framework

1. Spring MVC Controller

SpringMVCController.java

[code lang=”java”]
package javabeat.net.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping("/springtest")
public class SpringMVCController {

@RequestMapping(value="/redirectTest", method = RequestMethod.GET)
public String redirectTest() {
//some stuff here
return "redirect:/springtest/redirectedUrl";
}

@RequestMapping(value="/redirectedUrl", method = RequestMethod.GET)
public String redirection(Model model) {
//some stuff here
model.addAttribute("msg", "Spring Redirection Test");
return "hello";
}
}
[/code]

2. Views

hello.jsp

[code lang=”java”]
<html>
<body>
<h1>JavaBeat Spring MVC Examples</h1>
<h2>Spring MVC Examples : ${msg}</h2>
</body>
</html>[/code]

3. Spring Configurations

spring-dispatcher-servlet.xml

[code lang=”java”]
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:websocket="http://www.springframework.org/schema/websocket"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/websocket
http://www.springframework.org/schema/websocket/spring-websocket-4.0.xsd">

<context:component-scan base-package="javabeat.net.spring.controller" />

<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
[/code]

4. Web Deployment Descriptor

web.xml

[code lang=”java”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>Spring MVC 4.0 Web Application</display-name>
<servlet>
<servlet-name>spring-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
[/code]

5. Demo

When you run the above example, you would see the following output.

Spring MVC Redirection Example

 

Filed Under: Spring Framework Tagged With: Spring MVC

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved