Spring Boot 整合 Thymeleaf 模板 - 第005章 - 自定义资源目录

本文介绍如何在SpringBoot项目中整合Thymeleaf模板引擎,包括配置静态资源路径、创建样式文件并将其引入到页面中。通过具体步骤展示整个过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

视频课程地址: Spring Boot 整合 Thymeleaf 模板

  • springboot 的默认资源目录为 static/public/resources

  • 创建在源码目录下创建一个配置文件夹 config, 然后创建一个 StaticConfig 类文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * StaticConfig <br/>
 * 描述 : StaticConfig <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 创建时间 : 2018-03-19 下午3:19 <br/>
 * 联系作者 : <a href="mailTo:shichengoooo@163.com">qianmoQ</a>
 */
@Configuration
public class StaticConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // addResourceHandler中的是访问路径,可以修改为其他的字符串
        // addResourceLocations中的是实际路径
        registry.addResourceHandler("/sti/**").addResourceLocations("classpath:/sti/");
        super.addResourceHandlers(registry);
    }

}
  • 在resources目录下新建sti目录, 并在该目录下新建 a.css 样式
h1 {
    font-size: 100px;
}
  • 在页面文件引入该样式
<link rel="stylesheet" href="/sti/a.css" />
<think>好的,我现在要解决用户提出的Thymeleaf模板引擎未正常工作的问题。用户提到“thymeleaf不生效”,需要找出可能的原因及解决方案。首先,我需要回忆一下ThymeleafSpring Boot中的常见配置问题,并结合提供的引用料来整理可能的原因和解决办法。 首先,用户提供的引用中有多个相关的解决方案。例如,引用[2]提到无法找到模板位置,提示检查Thymeleaf配置。这可能意味着模板文件没有放在正确的目录下,或者配置中的前缀和后缀设置有误。根据引用[4],正确的配置应该是spring.thymeleaf.prefix=classpath:/templates/和spring.thymeleaf.suffix=.html。因此,我需要确认用户的项目结构是否符合这个要求,即HTML文件是否位于resources/templates目录下,并且后缀是否正确。 接下来,引用[1]和引用[3]都涉及到静态源加载的问题,比如CSS和JS文件无法加载。这可能涉及到Thymeleaf的语法是否正确,比如是否使用了th:href或th:src属性,以及是否正确引入了命名空间。例如,在HTML标签中需要声明xmlns:th="http://www.thymeleaf.org"。如果用户没有正确添加这些命名空间,Thymeleaf的标签可能不会被解析,导致源加载失败。 然后,引用[5]提到Shiro整合时的问题,需要添加方言才能使标签生效。虽然用户的问题可能不直接涉及Shiro,但这也提示了如果使用其他扩展(如Spring Security或自定义标签库),可能需要配置对应的方言。例如,Spring Security的thymeleaf-extras-springsecurity模块需要引入xmlns:sec命名空间,并且在配置中添加相应的方言Bean。 此外,缓存问题也是一个常见因素。在开发环境中,Thymeleaf默认会缓存模板,这可能导致修改后的页面无法及时生效。引用[4]中提到可以通过设置spring.thymeleaf.cache=false来禁用缓存,这在开发时非常有用。 还需要检查依赖是否正确引入。如果项目中缺少Thymeleaf的starter依赖,比如spring-boot-starter-thymeleaf,那么自动配置不会生效,导致模板引擎无法正常工作。此外,版本兼容性也可能是个问题,比如ThymeleafSpring Boot版本不匹配,可能需要调整依赖版本。 还有一个可能的点是控制器是否正确返回视图名称。如果控制器方法返回的字符串与模板文件名不匹配,或者没有正确使用@Controller注解,也可能导致Thymeleaf无法解析视图。 总结可能的原因: 1. 模板文件位置不正确。 2. Thymeleaf配置错误(前缀、后缀)。 3. 静态源路径错误或未使用Thymeleaf属性。 4. 未引入必要的命名空间。 5. 缓存未禁用导致修改未生效。 6. 缺少Thymeleaf依赖或版本问题。 7. 第三方集成(如Shiro、Security)未配置方言。 8. 控制器返回视图名称错误。 针对这些原因,需要逐步检查配置、项目结构、依赖、命名空间和缓存设置,确保每个环节都正确无误。同时,结合用户提供的引用中的解决方案,逐一验证可能的错误点,并应用相应的解决方法。</think>### Thymeleaf模板引擎未正常工作的原因及解决方案 #### 一、模板文件位置错误 **问题原因** Thymeleaf默认从$classpath:/templates/$目录读取模板文件[^4]。若HTML文件未放置在$resources/templates$目录下,或文件后缀不符合配置要求(默认.html),模板引擎将无法解析。 **解决方案** 1. 检查项目结构,确认HTML文件位于$resources/templates$目录 2. 核对$application.properties$中的配置: ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html ``` --- #### 二、静态源加载失败 **问题原因** 未使用Thymeleaf语法引用静态源,或未正确引入命名空间[^1][^3]。 **解决方案** 1. 在HTML文件中声明命名空间: ```html <html xmlns:th="http://www.thymeleaf.org"> ``` 2. 使用Thymeleaf源语法: ```html <link th:href="@{/css/style.css}" rel="stylesheet"> <script th:src="@{/js/app.js}"></script> ``` --- #### 三、缓存未禁用(开发环境) **问题原因** Thymeleaf默认启用模板缓存,导致修改后页面不更新[^4]。 **解决方案** 在$application.properties$中添加: ```properties spring.thymeleaf.cache=false ``` --- #### 四、第三方集成配置缺失 **问题原因** 使用Shiro、Spring Security等组件时,未配置对应的Thymeleaf方言[^5]。 **示例配置(Shiro)** ```java @Bean public ShiroDialect shiroDialect() { return new ShiroDialect(); } ``` **示例配置(Spring Security)** 在HTML中引入命名空间: ```html <html xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity5"> ``` --- #### 五、依赖缺失或版本冲突 **问题原因** 未引入$spring-boot-starter-thymeleaf$依赖,或版本与Spring Boot不兼容。 **解决方案** 1. 添加基础依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> ``` 2. 检查依赖树,排除冲突版本 --- #### 六、控制器配置错误 **问题原因** 控制器未返回正确的视图名称,或未使用@Controller注解。 **正确示例** ```java @Controller public class DemoController { @GetMapping("/") public String index() { return "index"; // 对应templates/index.html } } ``` --- #### 七、调试建议 1. 启用Spring Boot的$logging.level.org.thymeleaf=DEBUG$查看模板解析日志 2. 检查浏览器控制台,确认静态源是否加载成功 3. 使用IDE的"Clean and Rebuild"操作清除缓存
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qianmoQ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值