Spring 6 中 Thymeleaf 模板引擎与视图解析器配置

一、依赖准备

首先,需在项目中引入 Thymeleaf 及 Spring 整合相关依赖。以 Maven 为例,在 pom.xml 中添加以下依赖:

<!-- Thymeleaf 核心 -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.1.2.RELEASE</version> <!-- 适配 Spring 6 的版本 -->
</dependency>

<!-- Thymeleaf 与 Spring 6 整合 -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring6</artifactId>
    <version>3.1.2.RELEASE</version>
</dependency>

<!-- Spring Web 依赖(Spring 6 核心) -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>6.0.0</version>
</dependency>

注意thymeleaf-spring6 是专门适配 Spring 6 的模块,需与 Spring 版本保持兼容(建议使用 Thymeleaf 3.1.x 及以上版本)。

二、Java 配置方式(推荐)

Spring 6 推荐使用 Java 配置类替代 XML 配置,以下是 Thymeleaf 模板引擎和视图解析器的完整配置类:

1. 核心配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;

@Configuration
@EnableWebMvc // 启用 Spring MVC 功能
public class WebConfig implements WebMvcConfigurer {

    /**
     * 配置模板解析器(TemplateResolver)
     * 作用:指定模板文件的位置、前缀、后缀、编码等
     */
    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        // 设置模板文件前缀(默认从 classpath:/templates/ 目录加载)
        resolver.setPrefix("classpath:/templates/");
        // 设置模板文件后缀(默认 .html)
        resolver.setSuffix(".html");
        // 设置模板编码(建议 UTF-8)
        resolver.setCharacterEncoding("UTF-8");
        // 设置模板模式(HTML5 模式,支持 HTML5 标准)
        resolver.setTemplateMode(TemplateMode.HTML);
        // 是否缓存模板(开发环境建议关闭,生产环境开启)
        resolver.setCacheable(false); // 开发时关闭缓存,避免修改后需重启
        return resolver;
    }

    /**
     * 配置模板引擎(TemplateEngine)
     * 作用:整合模板解析器,处理模板渲染逻辑
     */
    @Bean
    public SpringTemplateEngine templateEngine(ITemplateResolver templateResolver) {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        // 关联模板解析器
        engine.setTemplateResolver(templateResolver);
        // 可添加额外配置(如方言、表达式工厂等)
        // engine.addDialect(new SpringSecurityDialect()); // 若集成 Spring Security
        return engine;
    }

    /**
     * 配置视图解析器(ViewResolver)
     * 作用:将 Controller 返回的逻辑视图名解析为 Thymeleaf 视图
     */
    @Bean
    public ViewResolver viewResolver(SpringTemplateEngine templateEngine) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        // 关联模板引擎
        resolver.setTemplateEngine(templateEngine);
        // 设置视图解析器优先级(数值越小优先级越高,避免与其他视图解析器冲突)
        resolver.setOrder(1);
        // 设置响应编码(需与模板解析器编码一致)
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
}

三、XML 配置方式(传统方式)

若项目仍需使用 XML 配置,可在 Spring 配置文件(如 spring-mvc.xml)中添加以下配置:

<!-- 配置视图解析器 -->
<bean id="thymeleafViewResolver" class="org.thymeleaf.spring6.view.ThymeleafViewResolver">
    <!-- 页面渲染完输出时用的编码,设为UTF-8避免中文乱码 -->
    <property name="characterEncoding" value="UTF-8"/>
    <!-- 如果有多个视图解析器(比如同时有JSP和Thymeleaf的),这个数字越小越优先用Thymeleaf -->
    <property name="order" value="1"/>
    <!-- 配置模板引擎,Thymeleaf解析页面全靠它 -->
    <property name="templateEngine">
        <bean class="org.thymeleaf.spring6.SpringTemplateEngine">
            <!-- 配置模板解析器,负责找页面文件、读文件内容 -->
            <property name="templateResolver">
                <bean class="org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver">
                    <!-- 页面文件存在哪里,这里是/WEB-INF/templates/文件夹下 -->
                    <property name="prefix" value="/WEB-INF/templates/"/>
                    <!-- 页面文件的后缀名,这里是.html(也可以改成其他的,比如.thtml) -->
                    <property name="suffix" value=".html"/>
                    <!-- 模板类型,这里是HTML页面(也支持JS、CSS等类型) -->
                    <property name="templateMode" value="HTML"/>
                    <!-- 读页面文件时用的编码,和上面保持一致,都是UTF-8 -->
                    <property name="characterEncoding" value="UTF-8"/>
                </bean>
            </property>
        </bean>
    </property>
</bean>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值