spring mvc: page redirect adjustment

My project name is hello,

A chapter2 directory is built under the src/main/java directory

There are three configuration files: web.xml, chapter2-servlet.xml, applicationContext.xml

The three configurations are as follows:

 web.xml

<web-app>

  <display-name>Archetype Created Web Application</display-name>
  
<!--Configuration file path-->
<context-param>
 	<param-name>contextConfigLocation</param-name>
	<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

  <servlet>  
    <servlet-name>chapter2</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>  
<servlet-mapping>  
    <servlet-name>chapter2</servlet-name>  
    <url-pattern>/</url-pattern>  
</servlet-mapping>   

</web-app>

  

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
					       http://www.springframework.org/schema/mvc/spring-mvc.xsd
					       http://www.springframework.org/schema/context
					       http://www.springframework.org/schema/context/spring-context.xsd">


<!-- ViewResolver -->  
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	<property name="prefix" value="/WEB-INF/jsp/"/>
	<property name="suffix" value=".jsp"/>
</bean>
       
</beans>   

  chapter2-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
					       http://www.springframework.org/schema/mvc/spring-mvc.xsd
					       http://www.springframework.org/schema/context
					       http://www.springframework.org/schema/context/spring-context.xsd">
      
 

<!-- Default annotation mapping support -->
<mvc:annotation-driven />
<!-- Static resources -->
<mvc:resources mapping="/pages/**" location="/pages/"/>

 <!-- Automatically scanned package name-->
<context:component-scan base-package="chapter2.*" />   
       
</beans>      

  

Next look at the code implementation:

WeboneController.java

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


@Controller
public class WeboneController {

	@RequestMapping(value="/webone/index", method=RequestMethod.GET)
	public String index()
	{
		return "webone_index";
		
	}
	
	// redirect jump
	@RequestMapping(value="/webone/redirect", method=RequestMethod.GET)
	public String redirect()
	{
		return "redirect:finalPage";
		
	}
	
	@RequestMapping(value="/webone/finalPage", method=RequestMethod.GET)
	public String finalPage()
	{
		return "webone_final";
		
	}
}

  

jsp page

webone_index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>webone index</title>
</head>
<body>

<form:form method="GET" action="/hello/webone/redirect">
<table>
	<tr>
		<td><input type="submit" value="重定向跳转"/> </td>
	</tr>
</table>
</form:form>



</body>
</html>

  webone_fina.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>web-one finalPage spring mvc redirect page<</title>
</head>
<body>



<h2>spring mvc redirect page</h2>




</body>
</html>

  

Actually my access address is as follows: http://localhost:8080/hello/webone/index

Click the jump button inside, it will automatically jump to: http://localhost:8080/hello/webone/finalPage

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326076576&siteId=291194637