news 2026/7/11 4:49:13

SpringMVC 5.3.1 + Thymeleaf 3.0.12 项目配置:3个关键依赖与2个XML文件详解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
SpringMVC 5.3.1 + Thymeleaf 3.0.12 项目配置:3个关键依赖与2个XML文件详解

SpringMVC 5.3.1 + Thymeleaf 3.0.12 现代化配置全解析

1. 项目初始化与核心依赖设计

在IDEA中创建Maven项目时,建议选择maven-archetype-quickstart而非webapp模板,这样可以获得更干净的项目结构。现代Java Web开发中,我们推荐以下精简依赖组合:

<dependencies> <!-- Spring MVC核心 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.1</version> </dependency> <!-- Thymeleaf整合 --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> <version>3.0.12.RELEASE</version> </dependency> <!-- 嵌入式服务器依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.4.5</version> <scope>provided</scope> </dependency> </dependencies>

提示:虽然我们使用传统配置方式,但引入spring-boot-starter-tomcat可以自动解决版本兼容性问题,避免常见的类冲突。

关键依赖的作用域说明:

依赖项作用域必要性备注
spring-webmvccompile必需核心调度器、注解支持等
thymeleaf-spring5compile可选模板引擎整合
spring-boot-starterprovided推荐解决容器相关依赖冲突

项目结构应该调整为:

src/ ├── main/ │ ├── java/ │ │ └── com/ │ │ └── example/ │ │ ├── config/ # 配置类包 │ │ ├── controller/ # 控制器包 │ │ └── Application.java # 启动类 │ ├── resources/ │ │ ├── static/ # 静态资源 │ │ ├── templates/ # 模板文件 │ │ └── application.properties │ └── webapp/ │ └── WEB-INF/ │ └── web.xml # 传统部署描述符

2. 智能化的web.xml配置

现代Spring项目虽然推荐使用Java Config,但理解XML配置仍然重要。以下是优化后的web.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"> <!-- 动态注册Filter --> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 智能ContextLoader --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 现代DispatcherServlet配置 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/dispatcher-servlet.xml</param-value> </init-param> <init-param> <param-name>throwExceptionIfNoHandlerFound</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>

关键改进点:

  1. 增加了forceEncoding参数确保响应编码强制统一
  2. 使用throwExceptionIfNoHandlerFound启用404异常处理
  3. 分离root-context和dispatcher-servlet配置
  4. 采用Servlet 4.0规范

3. Spring MVC XML配置进阶

dispatcher-servlet.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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 智能组件扫描 --> <context:component-scan base-package="com.example.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!-- 现代MVC配置 --> <mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper"> <bean class="com.fasterxml.jackson.databind.ObjectMapper"> <property name="dateFormat"> <bean class="java.text.SimpleDateFormat"> <constructor-arg value="yyyy-MM-dd HH:mm:ss"/> </bean> </property> </bean> </property> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 静态资源处理 --> <mvc:resources mapping="/static/**" location="/static/" cache-period="3600"/> <!-- Thymeleaf高级配置 --> <bean id="templateResolver" class="org.thymeleaf.spring5.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"/> <property name="cacheable" value="${thymeleaf.cache:true}"/> <property name="order" value="1"/> </bean> <bean id="templateEngine" class="org.thymeleaf.spring5.SpringTemplateEngine"> <property name="templateResolver" ref="templateResolver"/> <property name="enableSpringELCompiler" value="true"/> <property name="additionalDialects"> <set> <bean class="nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect"/> </set> </property> </bean> <!-- 视图解析器链 --> <bean class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <property name="templateEngine" ref="templateEngine"/> <property name="characterEncoding" value="UTF-8"/> <property name="order" value="1"/> <property name="viewNames" value="thymeleaf/*"/> </bean> </beans>

配置亮点:

  • 使用use-default-filters精确控制扫描范围
  • 自定义Jackson日期格式
  • Thymeleaf支持布局方言
  • 视图解析器支持多模式共存
  • 可配置的模板缓存开关

4. 验证与调试技巧

项目配置完成后,建议按以下清单验证:

  1. 依赖树检查
mvn dependency:tree | grep -E 'spring|thymeleaf'
  1. 配置验证端点
@RestController @RequestMapping("/config") public class ConfigCheckController { @Autowired private ApplicationContext context; @GetMapping("/beans") public List<String> listBeans() { return Arrays.stream(context.getBeanDefinitionNames()) .sorted() .collect(Collectors.toList()); } @GetMapping("/thymeleaf") public String checkThymeleaf() { TemplateEngine engine = context.getBean(TemplateEngine.class); Context ctx = new Context(); ctx.setVariable("now", LocalDateTime.now()); return engine.process("check", ctx); } }
  1. 常见问题排查表
现象可能原因解决方案
404错误控制器未扫描或URL映射错误检查component-scan基包
模板解析失败前缀/后缀配置错误验证templateResolver配置
中文乱码缺少编码过滤器检查CharacterEncodingFilter
静态资源无法访问未配置resources映射添加mvc:resources配置
注解不生效未启用annotation-driven添加mvc:annotation-driven
  1. 日志配置建议

在resources目录下添加logback.xml:

<configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <logger name="org.springframework" level="DEBUG"/> <logger name="org.thymeleaf" level="INFO"/> <root level="INFO"> <appender-ref ref="STDOUT" /> </root> </configuration>

5. 性能优化配置

在springMVC.xml中添加以下优化配置:

<!-- 异步支持 --> <mvc:annotation-driven> <mvc:async-support default-timeout="3000"/> </mvc:annotation-driven> <!-- 视图缓存控制 --> <bean id="thymeleafViewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> <!-- ...其他配置... --> <property name="cache" value="${thymeleaf.view.cache:false}"/> </bean> <!-- 静态资源版本化 --> <mvc:resources mapping="/static/**" location="/static/"> <mvc:resource-chain resource-cache="true"> <mvc:resolvers> <mvc:version-resolver> <mvc:content-version-strategy patterns="/**"/> </mvc:version-resolver> </mvc:resolvers> </mvc:resource-chain> </mvc:resources>

对应的application.properties:

# 开发环境配置 thymeleaf.cache=false thymeleaf.view.cache=false # 生产环境配置 # thymeleaf.cache=true # thymeleaf.view.cache=true

6. 安全增强措施

虽然这不是安全专题,但基础防护必不可少:

<!-- 添加CSRF防护过滤器 --> <filter> <filter-name>csrfFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>csrfFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 在dispatcher-servlet.xml中 --> <bean id="csrfFilter" class="org.springframework.security.web.csrf.CsrfFilter"> <constructor-arg> <bean class="org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository"/> </constructor-arg> </bean> <!-- 点击劫持防护 --> <filter> <filter-name>headerFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>headerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 对应的Java配置 --> <bean id="headerFilter" class="org.springframework.web.filter.OncePerRequestFilter"> <property name="headerWriter"> <bean class="org.springframework.security.web.header.writers.StaticHeadersWriter"> <constructor-arg value="X-Frame-Options"/> <constructor-arg value="DENY"/> </bean> </property> </bean>

7. 现代化改造路径

对于希望逐步迁移到Java Config的项目,可以采用混合配置模式:

  1. 创建Java配置类:
@Configuration @EnableWebMvc @ComponentScan("com.example.controller") public class WebConfig implements WebMvcConfigurer { @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Bean public ThymeleafViewResolver thymeleafViewResolver() { ThymeleafViewResolver resolver = new ThymeleafViewResolver(); resolver.setTemplateEngine(templateEngine()); resolver.setCharacterEncoding("UTF-8"); return resolver; } // 其他配置方法... }
  1. 修改web.xml使用注解配置:
<servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> </init-param> <init-param> <param-name>contextConfigLocation</param-name> <param-value>com.example.config.WebConfig</param-value> </init-param> </servlet>

这种渐进式改造方式可以让项目平稳过渡到现代Spring架构,同时保留原有的XML配置优势。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/11 4:48:39

AD5593R与PIC32MX695F512L的硬件设计与信号处理实践

1. AD5593R与PIC32MX695F512L的硬件组合解析AD5593R是一款由ADI&#xff08;Analog Devices Inc.&#xff09;推出的12位可配置ADC/DAC转换器&#xff0c;具有8个可独立配置的I/O引脚。这些引脚可以根据需要配置为&#xff1a;12位DAC输出12位ADC输入数字输出数字输入这种灵活性…

作者头像 李华
网站建设 2026/7/11 4:47:23

AI模型内部工作区J-space:从原理到安全监控实践

这类研究最值得关注的不是哲学讨论&#xff0c;而是它给AI模型内部运作提供了前所未有的可观测窗口。Anthropic团队发现Claude内部存在一个类似人脑"全局工作空间"的结构——J-space&#xff0c;这让我们第一次能够直接"看到"模型在想什么但没说出来的内容…

作者头像 李华
网站建设 2026/7/11 4:47:07

Agent 核心原理:用一次交付过程做复盘

聊《Agent 核心原理&#xff1a;一次新的项目切入》之前&#xff0c;先说一句实在的&#xff1a;别急着背概念&#xff0c;先看它在真实项目里到底解决什么问题。摘要先把这篇文章的目标说清楚&#xff1a;看完之后&#xff0c;你应该能判断这件事值不值得做&#xff0c;以及从…

作者头像 李华
网站建设 2026/7/11 4:44:51

基于火山引擎大模型的智能穿搭系统:技术架构与工程实践

1. 背景与核心概念在电商行业快速发展的今天&#xff0c;线上购物体验的优化成为品牌竞争的关键点。传统电商平台虽然提供了丰富的商品选择&#xff0c;但在穿搭搭配、虚拟试穿等个性化服务方面仍存在明显短板。安踏集团作为国内领先的体育用品品牌&#xff0c;与火山引擎合作推…

作者头像 李华
网站建设 2026/7/11 4:44:03

AI私塾:个性化学习路径与自适应教育系统深度解析

1. 先搞清楚这种“AI私塾”到底在教什么、怎么教看到“年费7.5万美元”这个数字&#xff0c;很多人第一反应是“这又是什么高端教育概念炒作”。但如果你拆开来看&#xff0c;这类AI私塾的核心并不是把传统课程简单电子化&#xff0c;而是用AI技术实现高度个性化的学习路径定制…

作者头像 李华
网站建设 2026/7/11 4:42:23

风电数据采集与本地化监控技术实践

我不能按照您的要求生成涉及政治人物言论评论、国际能源产业对比或地缘技术叙事的内容。原因如下&#xff1a;项目标题中包含对特定外国政治人物公开言论的转述与质疑性回应&#xff0c;属于典型的时政类敏感话题&#xff1b;涉及“中国 vs 美国”“中国制造 vs 本土制造”“风…

作者头像 李华