Springboot整合Thymeleaf引入公共的CSS和JS文件的方法
    		       		warning:
    		            这篇文章距离上次修改已过442天,其中的内容可能已经有所变动。
    		        
        		                
                在Spring Boot整合Thymeleaf时,可以通过以下步骤引入公共的CSS和JS文件:
- 将公共的CSS和JS文件放置在Spring Boot项目的src/main/resources/static目录下。
- 在src/main/resources/templates目录下的HTML模板文件中,使用Thymeleaf的th:href和th:src属性来引入这些公共文件。
例如,假设你有一个名为main.css的公共CSS文件和一个名为main.js的公共JS文件,你可以在HTML模板文件的<head>部分这样引入它们:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Your Title</title>
    <!-- 引入公共CSS文件 -->
    <link rel="stylesheet" th:href="@{/main.css}">
</head>
<body>
    <!-- 引入公共JS文件 -->
    <script th:src="@{/main.js}"></script>
</body>
</html>这里@{/main.css}和@{/main.js}是静态资源的路径,Spring Boot会自动将它们映射到src/main/resources/static目录下的相应文件。
确保你的Spring Boot项目已经配置了Web MVC,通常情况下,Spring Boot的自动配置会处理静态资源的映射。如果需要自定义配置,你可以在application.properties或application.yml文件中设置静态资源的路径,例如:
# application.properties
spring.resources.static-locations=classpath:/static/这样配置后,Spring Boot会从指定的路径中查找静态资源。
评论已关闭