文章目录
-
- 1.SpringMVC执行流程
- 2.安装Tomcat
- 3.创建maven项目
-
-
- 1.创建sun-springmvc模块(webapp)
- 2.查看是否被父模块管理
- 3.pom.xml引入依赖
- 4.目录
- 5.SunDispatcherServlet.java
- [6.web.xml 配置中央控制器](#6.web.xml 配置中央控制器)
- 7.启动Tomcat测试访问
-
1.SpringMVC执行流程
1.基础流程图
2.详细流程图
2.安装Tomcat
1.下载
2.解压到任意目录即可
3.IDEA配置Tomcat
1.配置Deloyment
2.配置Server
3.创建maven项目
1.创建sun-springmvc模块(webapp)
2.查看是否被父模块管理
3.pom.xml引入依赖
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>
<parent>
<groupId>com.sunxiansheng</groupId>
<artifactId>core_mechanisms</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>sun-springmvc</artifactId>
<packaging>war</packaging>
<name>sun-springmvc Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 作用范围表示在打包放到生产环境时不需要带上这个依赖,因为tomcat本身有 -->
<scope>provided</scope>
</dependency>
<!-- dom4j解析xml -->
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
<!-- commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- 引入日志 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>sun-springmvc</finalName>
</build>
</project>
4.目录
5.SunDispatcherServlet.java
java
package com.sunxiansheng.springmvc.servlet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* Description: 中央控制器(本质是一个Servlet)
* @Author sun
* @Create 2024/8/6 15:33
* @Version 1.0
*/
public class SunDispatcherServlet extends HttpServlet {
// 日志
private static final Logger log = LoggerFactory.getLogger(SunDispatcherServlet.class);
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("SunDispatcherServlet doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
log.info("SunDispatcherServlet doPost");
}
}
6.web.xml 配置中央控制器
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>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置中央控制器 -->
<servlet>
<servlet-name>SunDispatcherServlet</servlet-name>
<servlet-class>com.sunxiansheng.springmvc.servlet.SunDispatcherServlet</servlet-class>
<!-- 读取sunspringmvc.xml文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:sunspringmvc.xml</param-value>
</init-param>
<!-- 在Tomcat启动时就自动加载 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SunDispatcherServlet</servlet-name>
<!-- 拦截所有请求(因为是中央控制器) -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>