springboot整合thymeleaf以及静态资源访问的问题

1. 导入依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2. 配置yml

前缀后缀什么的配置其实不需要,自动配置好了,只要关缓存方便调试就行

spring:
    # thymeleaf
  thymeleaf:
  # 关闭缓存
    cache: false

3. 静态资源

static:放css,js(当然也可以放html)
template:放themeyeaf模板(也就是html)
不按规范来就容易报错
比如:
如果按照我下图的项目目录结构,访问http://localhost:8080/index.html
则可以直接跳转到index.html页面,但是访问http://localhost:8080/test.html就是404

那么如何访问test.html呢?
在controller里面这样写就行

 @GetMapping("/test")
    public String test(){
    
    
        return "test";
    }

在这里插入图片描述
静态资源的读取顺序
又高到低排序
在这里插入图片描述
classpath:/ 就是 resource目录

"classpath:/META-INF/resources/", 
"classpath:/resources/",
"classpath:/static/", 
"classpath:/public/" 
"/":当前项目的根路径

当然,你也可以在yml配置文件里面自定义文件访问路径,或者写拦截器
可以参考这篇:https://www.cnblogs.com/zzw-blog/p/10655386.html

4. thymeleaf中的css,js如何引入

首先在 < html > 标签中添加xmlsn,这样写代码才有提示

<html lang="en" xmlns:th="http://www.thymeleaf.org">

齐次。在< head>标签里引入css js。这里要注意除了jquery我引用的是cdn加速(我项目中没有导入这个)
其他的都是从/css或者/js目录下的。/就代表我的根目录(也就是classpath:/static/

<head>
	<link href="/css/weui.css" rel="stylesheet" type="text/css"/>
    <script src="https://unpkg.com/[email protected]/dist/ionicons.js"></script>
    <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    <script type="text/javascript" src="/js/weui.js"></script>
 <head/>

这样css和js就都引入了,再学习thymeleaf语法就可以直接开始写代码了(这里就不讲了)

猜你喜欢

转载自blog.csdn.net/qq_40733911/article/details/106686113
今日推荐