Thymeleaf

maven

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

首先必须将视图文件放在src/main/resources/templates下是安全的。

html

<!DOCTYPE HTML>
<!--加链接-->
<html xmlns:th="http://www.thymeleaf.org"> 
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
</body>
</html>

templates下面的HTML标签必须关闭,例如
如果不想关闭可以设置

1.导入依赖
<!-- templates 忽略 未关闭标签 -->
<dependency>
   <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.21</version>
</dependency>
	
2.设置application.properties
spring.thymeleaf.mode=LEGACYHTML5
语法详解
1.变量与字符串操作

在页面中输出值

1.1 th:text

可以将一个值放入到input标签的value中

1.2 th:value

字符串操作(内置对象)
注意语法:
1.调用内置对象一定要用#
2. 大部分的内置对象都以s结尾,如:strings,numbers,dates

判断字符串是否为空,如果为空返回true,否则false。

${#strings.isEmpty(key)}

判断字符串是否包含指定的子串,包含返回true,否则false

${#strings.contains('ajjjjj','T')}

判断单签字符串是否以子串开头,如果是返回true,否则返回false

${#strings.startsWith('abcdefgh','a')}

返回字符串的长度

${#strings.length(msg)}

查找子串的位置,并返回该子串的下标,没找到返回-1

${#strings.indexOf(msg,'h')}

截取字符串

${#strings.substring('msgg',2)}
${#strings.substring('msgg',2,3)}

字符串大小写

${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}
2 日期格式化处理

格式化日期,默认的以浏览器默认语言为格式化标准

${#dates.format(key)}

按自定义的格式做日期转换

${#dates.format(key,'yyy/MM/dd')}
${#dates.year(key)} 年
${#dates.month(key)} 月
${#dates.day(key)} 日
3 判断
3.1 th:if
<span th:if="${sex}=='男'">
    男
</span>
<span th:if="${sex}=='女'">
    女
</span>
3.2 th:switch
    <div th:switch="${id}">
        <span th:case="1">1</span>
        <span th:case="2">2</span>
        <span th:case="3">3</span>
        <span th:case="*">默认</span>
    </div>
4 迭代
 th:each
    List<Users> ls=new ArrayList<>();
    <tr th:each="u:${ls}">
        <td th:text="${u.id}"></td>
        <td th:text="${u.name}"></td>
    </tr>

    <tr th:each="u,var:${ls}">
        <td th:text="${var.index}"></td>
        <td th:text="${var.count}"></td>
        <td th:text="${var.size}"></td>
        <td th:text="${var.even}"></td>
        <td th:text="${var.odd}"></td>
        <td th:text="${var.first}"></td>
        <td th:text="${var.last}"></td>
    </tr>
     状态变量属性
    1.index:当前迭代器的索引从0开始
    2.count:当前迭代对象的计数从1开始
    3.size:被迭代对象的长度
    4.even/odd:布尔值,当前循环是否是偶数/奇数,从0开始
    5.first:布尔值,是否当前循环的第一条
    6.last:布尔值,是否当前循环的最后一条
 th:each迭代map
    Map<string,Users> map=new hashMap();
    <tr th:each="maps:${map}">
	    <td th:each="entry:${maps}" th:text="${entry.value.userid}"></td>
	    <td th:each="entry:${maps}" th:text="${entry.value.name}"></td>
    </tr>
5域对象操作

5.1HttpServletRequest

request.setAttribute("req","HttpServletRequest");
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span>

5.2HttpSession

request.getSession().setAttribute("sess","HttpSession")
Session:<span th:text="${session.sess}"></span>

5.3ServletContext

request.getSession().getServletContext().setAttribute("app","application")
Application:<span th:text="${application.app}"></span>
6url表达式语法

基本语法:@{}
6.1URL类型
6.1.1绝对路径

<a th:href="@{http://www.baidu.com}">绝对路径</a>

6.1.2相对路径
1)相对于当前项目的根
相对于项目的上下文的相对路径

<a th:href="@{/show}">相对路径</a>

2)相对于服务器路径的根(相当于在同一个Tomcat下的运行的两个项目)

<a th:href="@{~/project2/resour}">相对于服务器的根</a>

6.2在URL中实现参数传递

 <a th:href="@{/show(id=1,name=zhangsan)}">传参</a>

6.3 在URL中通过restful风格进行参数传递

<a th:href="@{/path/{id}/show}(id=1,name=zhangsan)">restful传参</a>
springboot HTML引用css静态文件

在这里插入图片描述

在这里插入图片描述

跳转页面

在这里插入图片描述
配置application.properties
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值