SpringMVC核心机制环境搭建

文章目录

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>
7.启动Tomcat测试访问
相关推荐
阑梦清川1 天前
SpringMVC案例学习(一)--计算器设计&&登录页面设计
java·登录页面·springmvc·案例计算器
B1nna1 天前
SpringMVC学习记录(三)之响应数据
java·学习·json·springmvc·jsp
ModelBulider2 天前
九、HttpMessageConverter
java·开发语言·后端·spring·springmvc
皮不卡球秋4 天前
SpringMVC
java·spring boot·spring·springmvc·springboot
灰色孤星A13 天前
后台管理系统的通用权限解决方案(十)如何自定义SpringMVC的参数解析器
java·springmvc·springboot·参数解析器
一朵忽明忽暗的云18 天前
【SSM-Day5】SpringMVC
spring·springmvc
Onlooker12920 天前
SpringMVC6-SpringMVC的视图
springmvc
云和数据.ChenGuang20 天前
第二章 SpringMVC
springmvc
太自由24 天前
SpringMVC之 文件上传和下载
spring·springmvc·resource·文件上传下载·文件下载·springmvc文件
lanssssss1 个月前
SpringMVC
java·开发语言·spring·springmvc·javaweb