1:首先创建一个maven项目,并在pom.xml文件中导入依赖
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zql</groupId>
<artifactId>sm01-hello</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>sm01-hello Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.32</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>sm01-hello</finalName>
</build>
</project>
最主要的依赖是servlet的依赖,以及spring-webmvc的依赖,注意版本问题,不要导入版本过高,我导入的适用于jdk8
servlet依赖必须4.0.1以下
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency>
spring-webmvc 版本不宜过高,5.3就可以
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.32</version> </dependency>
这些依赖都是从maven中央仓库下载的,下载地址如下:
2:要配置webapp目录下的web.xml文件,文件目录如图:
在web.xml文件中写如下代码:设置DispatcherServlet类用于接受请求,设置映射器文件为springmvc.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
3:在resources资源目录下创建springmvc.xml文件,用于UserContriller对象的管理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="/user" class="com.zql.controller.UserController"></bean>
</beans>
4:创建UserController类,这个类要实现Controller接口,并重写其中的handRequest方法,返回ModelAndView类型的对象
package com.zql.controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class UserController implements Controller {
@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mv=new ModelAndView();
mv.setViewName("/user.jsp");
mv.addObject("name","zhangsan");
mv.addObject("age",18);
return mv;
}
}
5:配置tomcat
点击右上角的配置
选择tomcat
选择你运行的项目
运行结果
简单的接受请求的SpringMVC项目就搭建好了