一、环境搭建
1、创建 web 项目并添加依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>springmvc1</groupId>
<artifactId>springmvc1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<release>14</release>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<warSourceDirectory>WebContent/WEB-INF</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- spring-web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.30.RELEASE</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
2、配置 WebContent/WEB-INF/web.xml
<?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" 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>springmvc</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 若不配置spring配置文件路径,则默认文件名为 springDispatcher-servlet.xml (servlet的name-servlet.xml)
默认路径为 WEB-INF/springDispatcher-servlet.xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 加载springmvc配置文件 -->
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<!-- 拦截所有请求,并交由springDispatcher控制请求-->
<url-pattern>/</url-pattern>
<!-- 拦截/index开头的请求,并交由springDispatcher控制请求
<url-pattern>/index</url-pattern> -->
<!-- 只拦截该求情,并交由springDispatcher控制请求
<url-pattern>/index/xx.jsp</url-pattern> -->
<!-- 只拦截.jsp结尾的请求,并交由springDispatcher控制请求
<url-pattern>.jsp</url-pattern> -->
</servlet-mapping>
</web-app>
3、创建springmvc配置文件springmvc.xml,该文件放置在classpath类路径下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!-- 开启扫描器 -->
<context:component-scan base-package="com.slq.controller"></context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 在返回值前添加前缀 -->
<property name="prefix" value="/views/"></property>
<!-- 在返回值后添加后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
二、页面跳转
1、创建 WebContent/index.jsp 初始页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="index/welcome">欢迎</a>
</body>
</html>
2、创建 WebContent/views/welcome.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
欢迎进入
</body>
</html>
3、创建控制跳转类,类中的 @RequestMapping("/index")
与方法中的 @RequestMapping("/welcome")
共同组成 /index/welcome 与请求的路径的一致。所以进入 welcome() 方法。返回值 welcome ,但 springmvc.xml 中配置了视图解析器 InternalResourceViewResolver
,所以在返回值中拼接前缀与后缀:最终转发页面为:views/welcome.jsp
@Controller
@RequestMapping("/index")
public class WelcomeServlet {
/**
* 跳转
* @return
*/
@RequestMapping("/welcome")
public String welcome() {
return "welcome";//默认使用转发方式跳转页面
}
}