html使用thymeleaf使用静态资源被拒绝
时间: 2023-08-19 17:03:53 浏览: 86
如果您使用Thymeleaf来处理HTML模板,并且发现无法加载静态资源,可能是由于Thymeleaf的资源解析器没有配置正确所致。您可以按照以下步骤来解决这个问题:
1. 确认静态资源的路径是否正确。请确保静态资源的路径与HTML模板中引用资源的路径一致。
2. 在Spring Boot的配置文件中,添加以下配置:
```
spring.thymeleaf.mode=HTML
spring.thymeleaf.cache=false
spring.thymeleaf.prefix=file:src/main/resources/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.resources.static-locations=file:src/main/resources/static/
```
这些配置将告诉Spring Boot在classpath下查找HTML模板,并将静态资源的路径设置为“src/main/resources/static/”。
3. 在HTML模板中使用Thymeleaf的标记来引用静态资源。例如:
```
<link th:href="@{/css/style.css}" rel="stylesheet" type="text/css"/>
<script th:src="@{/js/script.js}"></script>
```
这些标记将告诉Thymeleaf在静态资源的路径下查找相应的资源文件。
如果您仍然无法加载静态资源,请检查控制台是否有任何错误信息,并检查静态资源的文件权限是否正确。
阅读全文
相关推荐













