IDEA2025 搭建Web并部署到Tomcat运行Servlet+Thymeleaf

一、创建maven项目

二、pom.xml文件依赖包

XML 复制代码
  <dependencies>
        <!-- JUnit依赖 -->
        <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.9.2</version>
                <scope>test</scope>
        </dependency>
        <!-- Servlet依赖 -->
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>6.0.0</version>
        </dependency>
        <!-- Thymeleaf依赖 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

        <!-- lombok依赖 -->
        <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.32</version>
        </dependency>
    </dependencies>

三、选择File | Project Structrue | Facets | + 添加Web

选择模块

如图所示:web

四、编写HMTL页面和Servlet

(1) my.html

html 复制代码
<!DOCTYPE html>
<!--导入thymeleaf命名空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
 <div th:text="访问动态Thymeleaf页面">欢迎访问首页</div>
  <!-- 请求域取值-->
 <div th:text="${name}">张三</div>
</body>
</html>

(2) MyServlet.java 类 (CustomTemplateEngine类 略)

java 复制代码
package org.hlx.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hlx.thymeleaf.CustomTemplateEngine;
import org.thymeleaf.context.Context;

import java.io.IOException;

@WebServlet("/my")
public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("doGet()方法执行...");
        // 获取模板引擎对象
        CustomTemplateEngine templateEngine = CustomTemplateEngine.getInstance(req);
        // 创建数据模型,即将要传递给Thymeleaf的数据
        req.setAttribute("name", "张三");

        // 处理请求并响应结果
        templateEngine.processTemplate("my", req, resp);
    }

   protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

五、配置Tomcat10

(1)找到Edit Configurations...

(2)Tomcat Server | Local

(3) 选择已安装好的Tomcat10.1.41

(4) File | Project Structrue | Artifacts| + 添加Web部署

(5)可以修改部署文件名: Application context:

(6) 配置Tomcat VM options: 出现乱码

-Dfile.encoding=UTF-8

(7)配置Tomcat 运行时的更新方式

六、运行

相关推荐
Leinwin29 分钟前
OpenClaw 多 Agent 协作框架的并发限制与企业化规避方案痛点直击
java·运维·数据库
薛定谔的悦40 分钟前
MQTT通信协议业务层实现的完整开发流程
java·后端·mqtt·struts
enjoy嚣士1 小时前
springboot之Exel工具类
java·spring boot·后端·easyexcel·excel工具类
罗超驿1 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
盐水冰2 小时前
【烘焙坊项目】后端搭建(12) - 订单状态定时处理,来单提醒和顾客催单
java·后端·学习
凸头2 小时前
CompletableFuture 与 Future 对比与实战示例
java·开发语言
wuqingshun3141592 小时前
线程安全需要保证几个基本特征
java·开发语言·jvm
努力也学不会java3 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
攒了一袋星辰3 小时前
高并发强一致性顺序号生成系统 -- SequenceGenerator
java·数据库·mysql
小涛不学习3 小时前
Spring Boot 详解(从入门到原理)
java·spring boot·后端