SpringBoot---(10) SpringBoot集成 Thymeleaf 模板

1、SpringBoot对静态资源的映规则

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false) 
public class ResourceProperties implements ResourceLoaderAware { 
//可以设置和静态资源有关的参数,缓存时间等 
WebMvcAuotConfiguration:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
        if(!this.resourceProperties.isAddMappings()){
            logger.debug("Default resource handling disabled");
            return;
        }
        Integer cachePeriod=this.resourceProperties.getCachePeriod();
        if(!registry.hasMappingForPattern("/webjars/**")){
            customizeResourceHandlerRegistration(
                registry.addResourceHandler("/webjars/**")
                    .addResourceLocations(
                        "classpath:/META‐INF/resources/webjars/")
                    .setCachePeriod(cachePeriod));
        }
        String staticPathPattern=this.mvcProperties.getStaticPathPattern();
        //静态资源文件夹映射
        if(!registry.hasMappingForPattern(staticPathPattern)){
            customizeResourceHandlerRegistration(
                registry.addResourceHandler(staticPathPattern)
                    .addResourceLocations(
                        this.resourceProperties.getStaticLocations())
                    .setCachePeriod(cachePeriod));
        }
}
//配置欢迎页映射
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(
        ResourceProperties resourceProperties){
    return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
        this.mvcProperties.getStaticPathPattern());
}

//配置喜欢的图标
@Configuration
@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
public static class FaviconConfiguration {
    private final ResourceProperties resourceProperties;

    public FaviconConfiguration(ResourceProperties resourceProperties) {
        this.resourceProperties = resourceProperties;
    }

    @Bean
    public SimpleUrlHandlerMapping faviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        //所有 **/favicon.ico
        mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
                faviconRequestHandler()));
        return mapping;
    }

    @Bean
    public ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler = new
                ResourceHttpRequestHandler();
        requestHandler
                .setLocations(this.resourceProperties.getFaviconLocations());
        return requestHandler;
    }
}
(1)所有的所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

webjars:在SpringBoot配置资源中以 jar 包的方式引入静态资源;

<!--引入jQuery依赖-->
<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>jquery</artifactId>
    <version>3.6.0</version>
</dependency>
  • 其引入之后目录结构如下:
    在这里插入图片描述
  • 例如:访问路径localhost:8080/webjars/jquery/3.6.0/jquery.js
    在这里插入图片描述
(2)"/**" 访问当前项目的任何资源,都去(静态资源的文件夹)找映射
  • “classpath:/META‐INF/resources/”,
  • “classpath:/resources/”,
  • “classpath:/static/”,
  • “classpath:/public/”
  • “/”:当前项目的根路径
    在这里插入图片描述
  • 例如:访问localhost:8080/abc(则去静态资源文件夹里面找abc)
(3)欢迎页面:静态资源文件夹下的所有index.html页面,其都被“/**”映射
  • 例如:访问localhost:8080/(则去静态资源文件夹里面找对应的欢迎html页面)
    在这里插入图片描述
(4)所有的 **/favicon.ico 都是在静态资源文件下找
  • 例如:设置图标,将其放置在静态资源文件夹static下
    在这里插入图片描述

2、SpringBoot的Thymeleaf 模板引擎

在这里插入图片描述

(1)引入thymeleaf依赖
<!--SpringBoot 集成 Thymeleaf 的起步依赖-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<properties>
    <java.version>1.8</java.version>
    <!--切换thymeleaf版本-->
    <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
    <!-- 布局功能的支持程序 thymeleaf3主程序 需要layout2以上版本 -->
    <!-- thymeleaf2 layout1-->
    <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
(2)thymeleaf的使用

Thymeleaf底层源码:

@ConfigurationProperties(
    prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
    private static final Charset DEFAULT_ENCODING;
    public static final String DEFAULT_PREFIX = "classpath:/templates/";
    public static final String DEFAULT_SUFFIX = ".html";
    private boolean checkTemplate = true;
    private boolean checkTemplateLocation = true;
    private String prefix = "classpath:/templates/";
    private String suffix = ".html";
    private String mode = "HTML";

只要我们把HTML页面放在 classpath : / templates /,templates会自动渲染

在这里插入图片描述

  • 所以当我们把HTML页面放置在classpath : / templates / 目录下,thymeleaf就能自动渲染。例如:
@Controller
public class IndexController {

    @RequestMapping(value = "/index1")
    public String index(Model model) {
        model.addAttribute("data","SpringBoot 成功集成 Thymeleaf 模版!");
        return "index";
    }

}

在这里插入图片描述
在这里插入图片描述

(3)thymeleaf的表达式
  • 语法规则:
    在这里插入图片描述
1)th:text=" " 是 Thymeleaf 的一个属性,用于文本的显示
2)${变量名}:标准变量表达式,获取变量值
  • 例如:创建一个方法,将用户信息存放到model中,thymeleaf模板页面获取对象信息

A、在Controller 中添加方法,向 model 放入 User 对象

@RequestMapping(value = "/thymeleaf/user") 
public String user(Model model) {

	User user = new User(); user.setId(1);
	user.setName("张三");
	user.setPhone("13700000000"); user.setAddress("广州市");
	model.addAttribute("user",user); return "user";
}

B、在 templates 目录下创建 user.html 页面获取 User 对象数据

<h1>标准表达式:${ }</h1>
用户id:<span th:text="${user.id}">hcz</span><br>
用户姓名:<span th:text="${user.userName}">hcz</span><br>
用户年龄:<span th:text="${user.age}">hcz</span><br>
3)*{变量名}:选择表达式(不推荐使用)
  • 使用 th:object 属性来绑定对象,选择表达式首先使用 th:object来绑定后台传来的 User 对象,然后使用 * 来代表这个对象,后面 { } 中的值是此对象中的属性
  • 例如:在 user.html 通过选择变量表达式(星号表达式)获取用户数据
<h1>选择变量表达式(星号表达式):*{ }</h1>
<!--
    *{}必须使用th:object属性来绑定这个对象
    在div子标签中使用*来代替绑定的这个对象${user}
-->
<div th:object="${user}">
    用户id:<span th:text="*{id}">hcz</span><br>
    用户姓名:<span th:text="*{userName}">hcz</span><br>
    用户年龄:<span th:text="*{age}">hcz</span><br>
</div>
4)@{变量名}:URL表达式
  • 主要用于链接、地址的展示,可用于< script src="…" >、< link href="…">、< a href="…">、< form action="…">、< img src="">等,可以 在 URL 路径中动态获取数据
  • 例如:
    A、创建url.html
    <h1>URL路径表达式:@{...}</h1>
    <h2>a标签中的绝对路径(没有参数)</h2>
    <a href="http://www.baidu.com">传统写法:跳转到百度</a><br>
    <a th:href="@{http://www.baidu.com}">路径表达式:跳转到百度</a><br>
    <a th:href="@{http://localhost:8080/user/detail}">路径表达式:跳转到/user/detail</a><br>
    <a href="http://localhost:8080/user/detail">传统写法:跳转到/user/detail</a><br>
<hr>
    <h2>a标签中的相对路径(没有参数)</h2>
    <a th:href="@{/user/detail}">路径表达式:跳转到/user/detail</a><br>
    <a href="/user/detail">传统写法:跳转到/user/detail</a><br>
<hr>
    <h2>a标签中的绝对路径(有参数)(不推荐使用)</h2>
    <a href="http://localhost:8080/user/test?userName='hcz'">传统写法:跳转到/user/test?userName=''</a><br>
    <a th:href="@{http://localhost:8080/user/test?userName='zs'}">路径表达式:跳转到/user/test?userName=''</a><br>
<hr>
    <h2>a标签中的相对路径(有参数)</h2>
    <a href="/user/test?userName='hcz'">传统写法:跳转到/user/test?userName=''</a><br>
    <a th:href="@{/user/test?userName='zs'}">路径表达式:跳转到/user/test?userName=''</a><br>
<hr>
    <h2>a标签中的相对路径(有参数:从后台获取参数值)</h2>
    <a th:href="@{'/user/test1?id='+${id}+'&userName='+${userName}}">路径表达式:获取后台参数值</a><br>
    <a th:href="@{/user/test1(id=${id},userName=${userName})}">路径表达式:获取后台参数值(推荐使用该方式)</a><br>
    <a th:href="@{'/user/test2/'+${id}}">请求路径为RESTFul风格</a><br>
    <a th:href="@{'/user/test3/'+${id}+'/'+${userName}}">请求路径为RESTFul风格</a><br>
(4)thymeleaf的常用属性

请看下一节内容哦!!!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

@烟雨倾城ゝ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值