SpringMVC + Tomcat10

1. Tomcat 10的servlet包路径变了,javax -> jakarta
2. DispatcherServlet从Spring6 才开始使用jakarta.servlet.http.Servlet
3. Spring6 需要JDK 17+

1. 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.ityj.springmvc</groupId>
  <artifactId>spring_mvc</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>spring_mvc Maven Webapp</name>
     <properties>
        <java.version>17</java.version>
    </properties>
  <dependencies>
  
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>6.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.thymeleaf</groupId>
      <artifactId>thymeleaf-spring6</artifactId>
      <version>3.1.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>jakarta.servlet</groupId>
      <artifactId>jakarta.servlet-api</artifactId>
      <version>6.0.0</version>
    </dependency>

  </dependencies>
  <build>
    <finalName>spring_mvc</finalName>
  </build>
</project>

2. 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>

  <!--1、配置前端控制器,读取配置文件-->
  <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:application-springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>


</web-app>

3. Spring 配置

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

    <!--1、开启注解扫描-->
    <context:component-scan base-package="com.ityj.springmvc"/>

    <mvc:annotation-driven/>

    <!-- 2. 配置thymeleaf视图解析器-->
    <bean id="springResourceTemplateResolver" class="org.thymeleaf.spring6.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>

    <bean id="templateEngine" class="org.thymeleaf.spring6.SpringTemplateEngine">
        <property name="templateResolver" ref="springResourceTemplateResolver"/>
    </bean>
    <bean id="viewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine" ref="templateEngine"/>
    </bean>

</beans>

4. Controller

java 复制代码
package com.ityj.springmvc.controller;


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

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        System.out.println("hello");
        return "hello";
    }
}
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello</title>
</head>
<body>
Hello
</body>
</html>

6. 部署到Tomcat并测试

相关推荐
Moss Huang3 小时前
docker-runc not installed on system
java·docker·容器
麦兜*5 小时前
Spring Boot 集成 Docker 构建与发版完整指南
java·spring boot·后端·spring·docker·系统架构·springcloud
Cisyam^5 小时前
Go环境搭建实战:告别Java环境配置的复杂
java·开发语言·golang
CHENFU_JAVA5 小时前
使用EasyExcel实现Excel单元格保护:自由锁定表头和数据行
java·excel
青云交6 小时前
Java 大视界 -- 基于 Java 的大数据实时流处理在智能电网分布式电源接入与电力系统稳定性维护中的应用(404)
java·大数据·分布式·智能电网·flink 实时流处理·kafka 数据采集·iec 61850 协议
仰望星空@脚踏实地7 小时前
maven scope 详解
java·maven·scope
M_Reus_117 小时前
Groovy集合常用简洁语法
java·开发语言·windows
带刺的坐椅8 小时前
10分钟带你体验 Solon 的状态机
java·solon·状态机·statemachine
小鹅叻8 小时前
MyBatis题
java·tomcat·mybatis