关于thymeleaf配置语法运用 以及 静态资源问题总结 2021-06-08

在使用springBoot的时候一般用templates的包来放置静态资源,对于这个包来说它很特殊,这个包它需要导入thymeleaf的依赖才可以用它来和前后端做交互,下面将介绍一下它的使用方法:
导入thymeleaf依赖:

   <!-- thymeleaf模板引擎 我们都是基于3.x开发-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

然后就写controller和它交互吧,切记要把html文件放到resources下的templates包下。


对了,对于thymeleaf环境还可以在创建springBoot文件的时候就勾上
在这里插入图片描述
下面就是关于thymeleaf语法的一些梳理------>
关于thymeleaf传值和html交互的时候需要在导入th,下面代码介绍了遍历一级将标签连着数据一起显示的方法等等

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--所有的html元素都可以被thymeleaf替换接管:  th:元素名-->
<div th:text="${msg}+小姜"+></div>
<!--将标签也一起显示出来-->
<div th:utext="${msg}+小姜"+></div>
<hr>
<!--遍历-->
<!--<h3 th:each="user:${users}" th:text="${user}"></h3>-->
<h3 th:each="user:${users}">[[${user}]]</h3>
</body>
</html>

controller的代码

package cn.com.zzn;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

//在templates目录下的所有页面,只能通过controller来跳转!
//这个需要模板引擎支持!   thymeleaf

@Controller
public class IndexController {
    @RequestMapping("/test")
    public String index(Model model){
        model.addAttribute("msg","<h1>你好</h1>");
        model.addAttribute("users", Arrays.asList("zzn","zznshuojava"));
        return "test";
    }
}
关于学习总结在写一波:

一:分析源码找springBoot的自动配置
二:通过webjars去找文件所在的位置,比如找jquery的文件位置,还可以通过浏览
器看 http://localhost:8080/webjars/jquery/3.5.1/jquery.js注意导入依赖之后重启项目
三:springBoot2.5.0版本的classpath:/META-INF/等的其它四个这种类型的字符串数组一级继承在父类的WebProperties里面了
四:注意点 运行时要把application里的东西注释掉否则报错
五:资源优先级   resources>static(默认)>public   一般在public放一些公共资源
切记如果在application.properties中放了#spring.mvc.static-path-pattern=路径名  那么这些静态资源就无效了就访问不到了,就报错误页


六:总结 在springBoot中我们可以用以下方式处理静态资源
    1.webjars  需要用localhost:8080/webjars/
    2. public,static,/**,resources,   这三种直接映射到 localhost:8080/

这一节讲了首页和图标的问题
1.首页必须放到resources下的public或者resources包或者static下的包
  否则都访问不到,也可以放到template包下通过controller来访问,但是这个包需要配置模板引擎
2.就是图标了,新版本没有favicon.ico的配置信息,新版本需要到静态资源可以访问的三个包下,以前的老板本是可以直接
  放到根的resources下.

总结:看这些自动配置最好多看源码信息多看源码


关于thymeleaf模板引擎的学习
它的字都给配置类是ThymeleafProperties,切记你要导入一个类它一点存在一个properties
结论:只需要使用thymeleaf 只需要导入对应的依赖就可以了!我们将html放在我们的templates目录下
public static final String DEFAULT_PREFIX = "classpath:/templates/";
	public static final String DEFAULT_SUFFIX = ".html";

后端给前端传值的话,需要使用thymeleaf给html页面上添加<html lang="en" xmlns:th="http://www.thymeleaf.org">
然后通过th:元素名+后端的属性名
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值