- Spring - Home
- Spring - Overview
- Spring - Architecture
- Spring - Environment Setup
- Spring - Hello World Example
- Spring - IoC Containers
- Spring - Bean Definition
- Spring - Bean Scopes
- Spring - Bean Life Cycle
- Spring - Bean Post Processors
- Spring - Bean Definition Inheritance
- Spring - Dependency Injection
- Spring - Injecting Inner Beans
- Spring - Injecting Collection
- Spring - Beans Auto-Wiring
- Annotation Based Configuration
- Spring - Java Based Configuration
- Spring - Event Handling in Spring
- Spring - Custom Events in Spring
- Spring - AOP with Spring Framework
- Spring - JDBC Framework
- Spring - Transaction Management
- Spring - Web MVC Framework
- Spring - Logging with Log4J
Spring Useful Resources
Spring - Page Redirection Example
The following example show how to write a simple web-based application which makes use of redirect to transfer a http request to another page. To start with, let us have a working Eclipse IDE in place and take the following steps to develop a Dynamic Form based Web Application using Spring Web Framework −
Example - Page Redirection
| Steps | Description |
|---|---|
| 1 | Create a Maven project with archetype as maven-archetype-webapp with a name web, groupid com.tutorialspoint, artifactid web and create a package com.tutorialspoint under the src folder in the created project. |
| 2 | Update the pom.xml as explained in the Spring - MVC Hello World Example chapter. |
| 3 | Create a Java class WebController under the com.tutorialspoint package. |
| 4 | Create Spring configuration files web.xml and web-servlet.xml under the WEB-INF folder as explained in the Spring - MVC Hello World Example chapter. |
| 5 | Create a sub-folder with a name jsp under the WEB-INF folder. Create a view files index.jsp and final.jsp under this sub-folder. |
| 6 | The final step is to create the content of all the source and configuration files and export the application as explained below. |
WebController.java
package com.tutorialspoint;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebController {
@RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
@RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
return "redirect:finalPage";
}
@RequestMapping(value = "/finalPage", method = RequestMethod.GET)
public String finalPage() {
return "final";
}
}
index.jsp
Following is the content of Spring view file index.jsp. This will be a landing page, this page will send a request to access redirect service method which will redirect this request to another service method and finally a final.jsp page will be displauyed.
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring Page Redirection</title>
</head>
<body>
<h2>Spring Page Redirection</h2>
<p>Click below button to redirect the result to new page</p>
<form:form method = "GET" action = "/web/redirect">
<table>
<tr>
<td>
<input type = "submit" value = "Redirect Page"/>
</td>
</tr>
</table>
</form:form>
</body>
</html>
final.jsp
Following is the content of Spring view file final.jsp. This is the final redirected page.
<%@taglib uri = "http://www.springframework.org/tags/form" prefix = "form"%>
<html>
<head>
<title>Spring Page Redirection</title>
</head>
<body>
<h2>Redirected Page</h2>
</body>
</html>
Output
Once you are done creating the source and configuration files, export your application. Right-click on your application and use run as > maven install > option and save your web.war file in Tomcat's webapps folder.
Now start your Tomcat server and make sure you are able to access other web pages from webapps folder using a standard browser. Try a URL http://localhost:8080/web/index and you should see the following result if everything is fine with your Spring Web Application.
Click the "Redirect Page" button to submit the form and to get the final redirected page. You should see the following result if everything is fine with your Spring Web Application.