Thymeleaf 템플릿 엔진

무엇 thymeleaf 프레임 워크

  1. 설명 : Thymeleaf는 템플릿 엔진입니다
  2. 템플릿 엔진 : 기술적 사상의 동적 페이지의 콘텐츠를 달성하기는 (프런트 엔드 개발자는 백엔드 개발을 사용하는)
  3. 더 많은 인기 :

    A) 프리 마커

    b) 속도

    c) Thymeleaf

  1. 가장 중요한 특징

    a)  프로토 타입, 즉 인터페이스 미학

  1. 자연

    JSP + JSTL 대안

 

thymeleaf의 통합

  종속성을 소개

< 의존성 > 
    < 의 groupId > org.springframework.boot </ 의 groupId > 
    < artifactId를 > 스프링 부팅 스타터 thymeleaf </ artifactId를 > 
</ 의존성 >

 

모든 기본 구성 파일 

  개발 단계 : 사용 안 함 캐싱

 

thymeleaf 라벨을 사용하여

  일 : 텍스트  

    텍스트 교체   참고 : 문자열 연결 연산자 +

 

  일 : utext  

    텍스트 교체, 지원 HTML

 

  일 : 경우

    조건이있는 경우 만족하면, 현재 레이블을 표시하거나 표시하지 않습니다

 

  일 :하지 않는 한 부정 경우  를 제외

    않는 한 반대

 

  일 각

    순회

 

  일 : 스타일

 

표현

{}  $ ( OGNL의 표현 )

 

다른 범위의 값을 가져옵니다

  내장 객체

  날짜

  달력

  번호

  문자열

  사물

  ...

에센스 : 도구 그 패키지의 일부 방법

 

URL 표현

 

## 기본 속성 (대체)

  일 : 텍스트

  일 : utext

  일 : 스타일

참고 : 표현의 사용과 $ {}

 

두 가지 방법의 문자열 연결

  + 연산자

    내용에 스플 라이스 | 같은 | 안녕하세요 $ {세계} |

 

## 프로세스 분석 속성

  일 : 경우

  : 제 않는 ( 경우 반대 ) 

  일 : 스위치

  일 : 경우

참고 : 현재 레이블이 표시됩니다

 

## 주기 속성

  일 각

 

## 부동산의 특성

  일 : 객체

注:配合*表达式来取值,例如*{userName}

 

*表达式和$表达式的区别

  作用域不同,

  *表达式作用范围是子节点

  $表达式是整个页面

 

th:inline:指定当前标签内容的类型

None: 什么都不是

Text: 指定标签内部的是文本

Javascript: 指定标签内部的是js代码

 

## 链接属性

  Th:src

  Th:href

  Th:action

注:配合url表达式属性 例如@{}

 

## 代码复用属性

  th:include 把碎片节点中的内容插入当前节点

  th:replease 把碎片节点替换掉当前节点

  th:insert 把碎片节点插入到当前节点

  th:fragment //碎片

 

测试Thymeleaf中的标签

1.controller类

package com.seecen.thymeleaf.thymeleaf.controller;


import com.seecen.thymeleaf.thymeleaf.pojo.User;
import lombok.Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class HelloController {


    @RequestMapping("hello")
    public String hello(Model model, HttpSession session){
        int age = (int) (Math.random()*3+18);
        User user = new User();
        user.setUserId(1);
        user.setUserName("林大大");
        user.setAge(age);
        user.setCreateTime(new Date());

        User user2 = new User();
        user2.setUserId(2);
        user2.setUserName("嘤嘤嘤");
        user2.setAge(19);
        user2.setCreateTime(new Date());
        List<User> list = new ArrayList<>();
        list.add(user);
        list.add(user2);
        session.setAttribute("user",user);
        model.addAttribute("list",list);
        model.addAttribute("name","2333");
        model.addAttribute("msg","w(゚Д゚)w");
        return "hello";
    }
}

 

2.html页面(

xmlns:th="http://www.thymeleaf.org"      记住加

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<!--<head th:include="component::head('欢迎页面')">-->
<!--</head>-->
<body>

    <h2>thymeleaf</h2>
    <p th:text="'hello,'+ ${session.user.userName} + '!!'">嘤嘤嘤</p>  <!--文本替换-->

    <p th:text="|hello,${session.user.userName}, 另一种拼接方法 |">嘤嘤嘤</p>

    <p th:utext="|<a href='#'>hello, ${session.user.userName}</a>|"></p>


    <div>
        <span th:text="|欢迎 [${session.user.userName}] 使用xxx系统|"
        th:if="${session.user!=null}"
        >欢迎,[嘤嘤嘤] 使用xxx系统</span>
<!--        if的反转-->
        <a th:unless="${session.user==null}">退出</a>
    </div>

    <div>
        <table border="1">
            <tr>
                <td>编号</td>
                <td>姓名</td>
                <td>年龄</td>
                <td>操作</td>
            </tr>
            <tr th:each="user, S : ${list}"
                th:style="'background-color:'+${S.even?'blue':'aqua'}"
                style="background-color: aqua">
                <td th:text="${S.count}">编号</td>
                <td th:text="${user.userName}">姓名</td>
                <td th:text="${user.age}">年龄</td>
                <td>
                    <a href="#" th:href="@{'/update/'+${user.userId}}">修改</a>
                    <a href="#" th:href="@{|/update/${user.userId}}">修改</a>
                    <a href="#" th:href="@{/update/(id=${user.userId},age=${user.age})}">修改</a>
                    <a href="#">删除</a>
            </tr>
        </table>
    </div>

<div>
    <h2>thymeleaf内置对象</h2>
    <div th:text="${#dates.format(session.user.createTime,'yyyy-MM-dd')}">2000-1-1</div>
    <div th:text="${#dates.createNow()}"></div>
    <div th:text="${#dates.createToday()}"></div>
    <div th:text="${#dates.format(#dates.createNow(),'yyyy-MM-dd HH:mm:ss')}"></div>

<!--    字符串相关的-->
    <div th:text="'name属性的长度'+${#strings.length(name)}"></div>
    <div th:text="'world=WORLD?:'+${#strings.equalsIgnoreCase(name,'WORLD')}"></div>
    <div th:text="${#strings.randomAlphanumeric(6)}"></div>

<!--    数字相关-->
    <div th:text="${#numbers.formatDecimal(6666.666,1,2)}">0.99</div>

<!--    集合相关-->
    <div th:text="'#lists.size'+${#lists.size(list)}"></div>

<!--    url表达式-->
    <a href="#" th:href="@{/user/list}">用户列表页面</a>

<!--    流程判断-->
    <ul th:switch="${session.user.age}">
        <li th:case="${18}">18岁</li>
        <li th:case="${19}">19岁</li>
        <li th:case="20">20岁</li>
    </ul>

<!--    修改页面-->
    <div th:object="${session.user}">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" th:value="*{userName}"></td>  <!--临时变量-->
                <td><input type="text" th:value="${session.user.userName}"></td>
            </tr>
            <tr>
                <td>编号</td>
                <td><input type="password" th:value="*{userId}"></td>
            </tr>
            <tr>
                <td>年龄</td>
                <td><input type="text" th:value="*{age}"></td>
            </tr>
        </table>
    </div>

    <div>
        <p>hello [[${name}]]</p>
        <p th:inline="none">hello [[${name}]]</p>
        <script th:inline="text">  //<!--指定标签类型 text javascrpit-->
            // var msg = [[${msg}]];
            // var msg = '[[${msg}]]';
            alert(msg);
        </script>
    </div>

<div th:include="component/commons::copy"></div> <!--引入--> <div th:replace="component/commons::copy"></div> <!--替换--> <div th:insert="component/commons::copy"></div> <!--插入--> </div> </body> </html>

 

还一些标签不好展示  如(

 <div th:include="component/commons::copy"></div>  <!--引入-->
    <div th:replace="component/commons::copy"></div>  <!--替换-->
    <div th:insert="component/commons::copy"></div>  <!--插入-->

 这几个    

 

3.效果

 

还有很多标签

 

추천

출처www.cnblogs.com/lin02/p/11485467.html