spring mvc源码学习笔记之一

  • pom.xml 如下
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.qs.demo</groupId>
    <artifactId>test-007</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>war</packaging>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.30.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.11.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  • src/main/webapp/WEB-INF/web.xml 内容如下
xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <servlet>
        <servlet-name>app</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 自己指定,不用默认的 <servlet-name>-servlet.xml -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/a.xml</param-value>
        </init-param>
        <!-- 小小优化,加快首次访问速度 -->
        <load-on-startup>0</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>app</servlet-name>
        <!-- / 表示除了 xxx.jsp 之外的所有请求 -->
        <!-- /* 表示所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  • src/main/webapp/WEB-INF/a.xml 内容如下
xml 复制代码
<?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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 这个文件的名字是有讲究的 -->
    <!-- springmvc 的配置 -->

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="com.qs.demo"/>

    <!-- 配置视图解析器 -->
    <bean id="thymeleafViewResolver" class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
        <property name="characterEncoding" value="UTF-8"/>
        <property name="order" value="1"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring4.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>

</beans>
  • src/main/webapp/WEB-INF/templates/t01.html 内容如下
html 复制代码
<!DOCTYPE html>
<html lang="en" xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>t01</title>
</head>
<body>
    <a th:href="@{/t02}">hello</a>
</body>
</html>
  • src/main/webapp/WEB-INF/templates/t02.html 内容如下
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>t01</title>
</head>
<body>
   <h1>Peter</h1>
</body>
</html>
  • com.qs.demo.FirstController 内容如下
java 复制代码
package com.qs.demo;

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

/**
 * @author qs
 * @date 2024/12/20
 */
@Controller
public class FirstController {

  @RequestMapping("/t01")
  public String t01() {
    return "t01";
  }

  @RequestMapping("/t02")
  public String t02() {
    return "t02";
  }

}

以上就是全部代码

写这个例子只要是为了看 DispatcherServlet 的无参构造方法。如下

java 复制代码
/**
 * 创建一个 DispatcherServlet。
 * 这个 DispatcherServlet 将会依据默认值和通过 servlet init-param 指定的值创建一个属于自己的内部的 web 应用上下文。
 * 这个无参的构造方法通常在 servlet 2.5 及之前的环境中使用,在 servlet 2.5 及之前的环境中注册 servlet 的唯一方式是通过 web.xml,而通过 web.xml 注册需要使用无参构造。
 *
 * Create a new {@code DispatcherServlet} that will create its own internal web
 * application context based on defaults and values provided through servlet
 * init-params. Typically used in Servlet 2.5 or earlier environments, where the only
 * option for servlet registration is through {@code web.xml} which requires the use
 * of a no-arg constructor.
 *
 * 名为 contextConfigLocation 的 servlet init-param 对应的是 setContextConfigLocation 方法。
 * 它决定了 DEFAULT_CONTEXT_CLASS(默认是 XmlWebApplicationContext)会加载哪个 XML 文件。
 * 在本文的例子中,我们指定的是 /WEB-INF/a.xml。其实还可以通过这个值指定多个配置文件,比如:"/WEB-INF/a.xml,/WEB-INF/b.xml"。
 * 可以从 DispatcherServlet 的父类 FrameworkServlet 了解到这些。其他文章会讲。
 *
 * <p>Calling {@link #setContextConfigLocation} (init-param 'contextConfigLocation')
 * will dictate which XML files will be loaded by the
 * {@linkplain #DEFAULT_CONTEXT_CLASS default XmlWebApplicationContext}
 *
 * 名为 contextClass 的 servlet init-param 对应的是 setContextClass 方法。
 * 指定了这个 servlet init-param 的话,默认的 XmlWebApplicationContext 就被覆盖了。
 * 可以用这个 servlet init-param 来指定用其他应用上下文,比如 AnnotationConfigWebApplicationContext。
 * 其他文章会演示。
 *
 * <p>Calling {@link #setContextClass} (init-param 'contextClass') overrides the
 * default {@code XmlWebApplicationContext} and allows for specifying an alternative class,
 * such as {@code AnnotationConfigWebApplicationContext}.
 * 
 * 名为 contextInitializerClasses 的 servlet init-param 对应的是 setContextInitializerClasses 方法。
 * 它指定了在本servlet的内部的web应用上下文的 refresh() 方法被调用之前可以使用哪些 ApplicationContextInitializer 来对web应用上下文进行配置。
 * 这也是个知识点。其他文章会详细讲。包括 ApplicationContextInitializer 这个接口,非常重要。
 * 
 * <p>Calling {@link #setContextInitializerClasses} (init-param 'contextInitializerClasses')
 * indicates which {@code ApplicationContextInitializer} classes should be used to
 * further configure the internal application context prior to refresh().
 * @see #DispatcherServlet(WebApplicationContext)
 */
public DispatcherServlet() {
    super(); // 不要忽略这个对父类无参构造方法的调用。它也很重要。对应的文档也值得好好看。
    System.out.println("看父类构造方法的javadoc");
    setDispatchOptionsRequest(true);
    System.out.println("这个构造方法一般在 servlet 2.5 及之前版本使用");
}
相关推荐
踏着七彩祥云的小丑13 小时前
Go学习第1天:入门
开发语言·学习·golang·go
憧憬成为web高手14 小时前
[0CTF 2016]piapiapia
学习
imDwAaY14 小时前
贝叶斯网络到粒子滤波Python算法实现 CS188 Proj4 学习笔记
网络·人工智能·笔记·python·学习·算法
我想我不够好。14 小时前
挖掘机技能介绍
学习
咸甜适中15 小时前
rust语言学习笔记Trait(十五)Drop(释放资源)
笔记·学习·rust
starsky7623815 小时前
基于 Spring AI 构建具备记忆与情绪的多角色 Agent 系统
人工智能·spring·架构
IT笔记15 小时前
【Rust】 Rust宏学习笔记
笔记·学习·rust
网络与设备以及操作系统学习使用者16 小时前
路由器如何实现跨VLAN通信
运维·网络·学习·华为·智能路由器
王五周八16 小时前
玩转 Spring AI Agent:基于 SpringBoot 集成 AI 工具与 Skills 能力实践
java·spring
182******208316 小时前
2026年学C语言还有出路吗?学习需要报班吗?
c语言·开发语言·学习