zoukankan      html  css  js  c++  java
  • thymeleaf--简单使用

    环境:spring-boot

    作用之一:jsp有jstl标签库。html就可以用thymeleaf完成数据显示,以及交互。

    使用

    1.添加依赖

    <properties>
            <!-- thymeleaf2   layout1-->
            <thymeleaf-layout-dialect.version>2.4.1</thymeleaf-layout-dialect.version>
    </properties>

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

    2.在templates文件夹下新建.html文件

    在html的头中引入:

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

    3.使用语法

    controller:

    package com.org.controller;
    
    import com.org.pojo.dog;
    import com.org.pojo.person;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import javax.servlet.http.HttpServletRequest;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    @Controller
    public class thymeleafController {
    
        @RequestMapping("testyh")
        public String testyh(HttpServletRequest request){
            request.setAttribute("name","admin");
            ArrayList<Object> objects = new ArrayList<>();
            objects.add("admin");
            objects.add(18);
            objects.add("男");
            request.setAttribute("list", objects);
            request.setAttribute("p", new person("admin"));
            return "thymeleaf";
        }
    }

    访问:http://localhost/testyh

    thymeleaf.html内容:(我新建的html)

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>
    [[(${name})]]
    </body>
    </html>

  • 相关阅读:
    Android测试入门篇
    SQL的基本知识
    正则表达式
    ES5语法
    vscode
    继承小结
    工作遇到的问题
    后台程序员的HTTP缓存
    xhr下载图片/服务器向客户端推送消息
    HTTP2.0
  • 原文地址:https://www.cnblogs.com/yangzhuxian/p/12787737.html
Copyright © 2011-2022 走看看