Thymeleaf 个人笔记

视图应用

资源(模板来源 )上下文(变量来源 )模板引擎(模板渲染)

通用资源抽象:File、ClassLoader、URL、ServletContext

Spring 资源抽象:Resource

Thyemeaf:Context

Spring Web MVC:Model

Servlet:Attribute

Thymeleaf:TemplateEngine

Spring:SpringTemplateEngine

Spring WebFlux:SpringWebFluxTemplateEngine

 

 

 

 

模板来源:Resource。变量来源:Context。模板渲染:SpringTemplateEngine

public class ThymeleafTemplateEngineBootstrap {

    public static void main(String[] args) throws IOException {
        // 构建引擎
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        // 创建渲染上下文
        Context context = new Context();
        context.setVariable("message", "Hello,World");

        // 读取内容从 classpath:/templates/thymeleaf/hello-world.html
        // ResourceLoader
        ResourceLoader resourceLoader = new DefaultResourceLoader();
        // 通过 classpath:/templates/thymeleaf/hello-world.html Resource
        Resource resource = resourceLoader.getResource("classpath:/templates/thymeleaf/hello-world.html");
        File templateFile = resource.getFile();
        // 文件流
        FileInputStream inputStream = new FileInputStream(templateFile);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        // Copy
        IOUtils.copy(inputStream, outputStream);

        inputStream.close();
        // 模板的内容
        String content = outputStream.toString("UTF-8");
        // 渲染(处理)结果
        String result = templateEngine.process(content, context);
        // 输出渲染(处理)结果
        System.out.println(result);
    }
}

 

Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本。

1、六种模式格式

  • HTML (网页)
  • XML (配置)
  • TEXT (文本)
  • JAVASCRIPT (js)
  • CSS (样式)
  • RAW (无格式)

2、简单demo

  • pom
        <!-- Thymeleaf 自动配置 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>        
        <!-- 允许使用非严格的 HTML 语法 -->
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>
  • application.yml
spring:
  thymeleaf:
    cache: false # 开发时关闭缓存,不然没法看到实时页面
    mode: HTML # 用非严格的 HTML
    encoding: UTF-8
    servlet:
      content-type: text/html
public class PersonBean implements Serializable {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}
@Controller
@RequestMapping(value = "thymeleaf")
public class IndexController {

    @RequestMapping(value = "index", method = RequestMethod.GET)
    public String index(Model model) {
        PersonBean person = new PersonBean();
        person.setName("张三");
        person.setAge(22);

        List<PersonBean> people = new ArrayList<>();
        PersonBean p1 = new PersonBean();
        p1.setName("李四");
        p1.setAge(23);
        people.add(p1);

        PersonBean p2 = new PersonBean();
        p2.setName("王五");
        p2.setAge(24);
        people.add(p2);

        PersonBean p3 = new PersonBean();
        p3.setName("赵六");
        p3.setAge(25);
        people.add(p3);

        model.addAttribute("person", person);
        model.addAttribute("people", people);

        return "index";
    }
}

 resources/templates/index.html

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Hello Thymeleaf</title>
</head>
<body>
<div>
    <span>访问 Model:</span><span th:text="${person.name}"></span>
</div>
<div>
    <span>访问列表</span>
    <table>
        <thead>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
        </tr>
        </thead>
        <tbody>
        <tr th:each="human : ${people}">
            <td th:text="${human.name}"></td>
            <td th:text="${human.age}"></td>
        </tr>
        </tbody>
    </table>
</div>
</body>
</html>

使用语法

https://www.jianshu.com/p/908b48b10702

Spring Boot是一个用于创建独立的、基于Spring的应用程序的框架,它简化了Spring应用程序的开发过程。Thymeleaf是一种Java模板引擎,它可以在Web应用程序中生成动态的HTML页面。个人博客是一个用于展示个人观点、经验和知识的网站。 在Spring Boot中使用Thymeleaf来创建个人博客非常方便。你可以使用Thymeleaf来定义HTML模板,并在模板中使用Thymeleaf的标签和表达式来动态地渲染数据。同时,Spring Boot提供了许多功能和组件,如数据库访问、安全认证等,可以帮助你构建一个完整的个人博客应用程序。 以下是使用Spring Boot和Thymeleaf创建个人博客的一般步骤: 1. 创建一个Spring Boot项目,并添加所需的依赖,包括Thymeleaf和其他必要的组件。 2. 创建数据库表格来存储博客文章、用户信息等。 3. 创建实体类来映射数据库表格,并编写相应的Repository接口来进行数据库操作。 4. 创建控制器类来处理用户请求,并编写相应的处理方法。 5. 创建Thymeleaf模板来定义博客页面的布局和样式,并使用Thymeleaf的标签和表达式来动态地展示数据。 6. 在控制器类中调用Repository接口来获取数据,并将数据传递给Thymeleaf模板进行渲染。 7. 配置Spring Security来实现用户认证和授权功能,以保护博客的安全性。 8. 部署和运行你的个人博客应用程序。 希望以上介绍对你有所帮助!如果你有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值