IDEA2025 基于 Jakarta EE 开发 Servlet + Thymeleaf

📅 发布时间:2026/7/12 20:13:55 👁️ 浏览次数:
IDEA2025 基于 Jakarta EE 开发 Servlet + Thymeleaf
一、前提准备确保 IDEA 已配置JDK 17Servlet 6.0 要求。已配置Tomcat 10Jakarta EE 适配版本Servlet 6.0 需 Tomcat 10.1。项目依赖已正确引入Thymeleaf 3.1.2 Servlet 6.0参考之前的 pom.xml。!-- Thymeleaf依赖 -- dependency groupIdorg.thymeleaf/groupId artifactIdthymeleaf/artifactId version3.1.2.RELEASE/version /dependency !-- lombok依赖 -- dependency groupIdorg.projectlombok/groupId artifactIdlombok/artifactId version1.18.32/version /dependency !-- druid -- dependency groupIdcom.alibaba/groupId artifactIddruid/artifactId version1.2.16/version /dependency !-- mysql驱动 -- dependency groupIdmysql/groupId artifactIdmysql-connector-java/artifactId version8.0.28/version /dependency4. 创建工程二、核心配置步骤步骤 1配置 Thymeleaf 模板路径关键Thymeleaf 的模板文件默认放在src/main/webapp/WEB-INF/templates/下需手动创建目录这一步要确保 Thymeleaf 工具类的模板解析器配置正确步骤 2编写 Servlet 并映射访问路径以「首页页面」为例编写 Servlet 并通过WebServlet注解配置访问路径替代 web.xml 配置更简洁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.servlet.base.CustomTemplateEngine; import java.io.IOException; /** * author : HLX * ClassName :IndexServlet * date : 2026/3/7 15:41 * Version :1.0 * Description: TODO * modyified By : */ WebServlet(name index, value /index) public class IndexServlet extends HttpServlet { Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { CustomTemplateEngine customTemplateEngine CustomTemplateEngine.getInstance(req); req.setAttribute(hello, Servlet Thymeleaf搭建); customTemplateEngine.processTemplate(index, req, resp); } Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }步骤 3创建 Thymeleaf 模板文件在src/main/webapp/WEB-INF/templates/下创建index.html对应 Servlet 中的模板名index!DOCTYPE html html langen xmlns:thhttp://www.thymeleaf.org head meta charsetUTF-8 titleTitle/title /head body Welcome to the index page!p/ h1 th:text${hello}Hello/h1 /body /html步骤 4配置 IDEA 项目部署关键打开 IDEA →Run → Edit Configurations→ 选择 Tomcat 10 配置。切换到Deployment选项卡点击→ 选择Artifact→ 选择你的项目格式为xxx:war exploded必须是「解压后的 war 包」。设置Application context项目访问根路径建议设为/方便访问或自定义如/book02。切换到Server选项卡确保URL是http://localhost:8080/对应 Application context 为/或http://localhost:8080/book02/对应自定义路径。勾选After launch启动后自动打开浏览器。四、常见问题排查访问失败必看问题 1404 错误页面找不到检查 Servlet 注解WebServlet(/index)是否拼写正确。检查 Thymeleaf 模板路径/WEB-INF/templates/index.html是否存在目录名、文件名大小写一致。检查 Tomcat 部署的 Application context 是否正确比如配置了/book02访问时必须加这个前缀。问题 2500 错误页面渲染失败检查 Thymeleaf 工具类的resolver.setPrefix(/WEB-INF/templates/)是否以/开头且路径正确。检查 Servlet 中ThymeleafUtil.process(book/list, ...)的模板名是否和模板文件路径匹配book/list对应templates/book/list.html。检查 Thymeleaf 模板中的表达式是否正确比如${books}是否为 null可加非空判断th:if${not #lists.isEmpty(books)}。问题 3静态资源CSS/JS404静态资源放在src/main/webapp/static/下访问路径为/static/xxx比如/static/lib/bootstrap-3.4.1/css/bootstrap.min.css。确保 Tomcat 部署的是war exploded包静态资源会被正确加载。总结核心路径对应关系Servlet 注解路径WebServlet(/book/list)→ 访问 URLhttp://localhost:8080/book/list。Thymeleaf 模板名book/list→ 模板文件路径/WEB-INF/templates/book/list.html。关键配置Thymeleaf 模板解析器必须正确设置prefix/WEB-INF/templates/和suffix.html。Tomcat 部署需选择war exploded包并配置正确的 Application context。访问规则最终访问 URL Tomcat地址 Application context Servlet映射路径。