您的位置:首页 > 教育 > 培训 > 深圳独立设计工作室_建行网站用户名是什么_苏州seo推广_青岛百度seo代理

深圳独立设计工作室_建行网站用户名是什么_苏州seo推广_青岛百度seo代理

2025/6/9 21:48:11 来源:https://blog.csdn.net/m0_46695127/article/details/143096789  浏览:    关键词:深圳独立设计工作室_建行网站用户名是什么_苏州seo推广_青岛百度seo代理
深圳独立设计工作室_建行网站用户名是什么_苏州seo推广_青岛百度seo代理

文章目录

    • pom.xml
    • ThymeleafTest.java
    • hello.html
    • footer.html
  • springboot整合Thymeleaf
    • pom.xml
    • HelloController2.java

Thymeleaf是一个现代化服务器侧Java模板引擎,用于Web和独立环境
Thymeleaf的主要目标是为工作流程带来优雅的自然模板- HTML 可正确显示在浏览器中,也可以作为静态原型工作,从而在开发团队中实现更强的合作
Thymeleaf拥有Spring框架模块、大量与工具的集成以及插入自己功能的能力,是现代HTML5 JVM Web开发的理想之选

  1. th:text
    设置当前元素中的文本内容,原样输出
    th:utext
    设置当前元素中的文本内容会解析其中的内容
  2. th:each
    遍历集合中的数据
  3. th:if 判断是否为真
    th:unless 判断是否为假
  4. th:insert 引入模板片段
    th:fragment:定义一个片段

pom.xml

        <dependency><groupId>org.thymeleaf</groupId><artifactId>thymeleaf</artifactId><version>3.0.11.RELEASE</version></dependency>

ThymeleafTest.java

package com.xd.cubemall.tpl;import com.xd.cubemall.search.model.User;
import org.junit.Test;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;public class ThymeleafTest {@Testpublic void testThymeleaf() throws Exception {//创建一个基于classpath的一个加载器ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();//设置加载器的属性,前缀和后缀templateResolver.setPrefix("templates/");templateResolver.setSuffix(".html");//创建一个模板引擎TemplateEngine engine = new TemplateEngine();engine.setTemplateResolver(templateResolver);//创建一个Context对象,相当于是一个map,向模板传递数据需要使用Context context = new Context();//向content对象中添加模板使用的变量context.setVariable("hello","hello world!");context.setVariable("html","<h1>hello</h1>");List<User> userList = new ArrayList<>();userList.add(new User(1, "张三1","北京",new Date()));userList.add(new User(2, "张三2","北京",new Date()));userList.add(new User(3, "张三3","北京",new Date()));userList.add(new User(4, "张三4","北京",new Date()));userList.add(new User(5, "张三5","北京",new Date()));userList.add(new User(6, "张三6","北京",new Date()));userList.add(new User(7, "张三7","北京",new Date()));context.setVariable("userList",userList);context.setVariable("flag",false);//渲染模板,模板所在的位置,使用的context对象,静态文件生成的路径FileWriter writer = new FileWriter("D:/p4/hello.html");engine.process("hello",context,writer);}
}

hello.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>hello</title>
</head>
<body><div th:text="${hello}"></div><div th:text="${html}"></div><div th:utext="${html}"></div><table border="1"><tr><th>id</th><th>name</th><th>address</th><th>birthday</th></tr><tr th:each="u:${userList}"><td th:text="${u.id}"></td><td th:text="${u.name}"></td><td th:text="${u.address}"></td><td th:text="${#dates.format(u.birthday,'yyyy/MM/dd')}"></td></tr></table><div th:if="${flag}">这是flag为true时显示</div><div th:unless="${flag}">这是flag为false时显示</div><div th:insert="~{footer::footer}"></div>
</body>
</html>

footer.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><div th:fragment="footer"><h5>这是页脚内容~~~</h5></div>
</body>
</html>

springboot整合Thymeleaf

创建模板
模板文件要求放到
resources/templates目录下
模板文件的扩展名默认是html
使用模板
在controller方法中返回一个String数据类型,返回模板的名称,springboot会自动找到对应的模板文件
如果需要向模板传递数据,可以使用Model对象传递

pom.xml

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

HelloController2.java

package com.xd.cubemall.search.controller;import com.xd.cubemall.search.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;import java.util.ArrayList;
import java.util.Date;
import java.util.List;@Controller
public class HelloController2 {//可以使用SpringTemplateEngine生成静态页面//@Autowired//private SpringTemplateEngine engine;@GetMapping("/html/hello")//返回String类型,默认使用springmvc的视图解析器public String sayHello(Model model) {//向content对象中添加模板使用的变量model.addAttribute("hello","hello world!");model.addAttribute("html","<h1>hello</h1>");List<User> userList = new ArrayList<>();userList.add(new User(1, "张三1","北京",new Date()));userList.add(new User(2, "张三2","北京",new Date()));userList.add(new User(3, "张三3","北京",new Date()));userList.add(new User(4, "张三4","北京",new Date()));userList.add(new User(5, "张三5","北京",new Date()));userList.add(new User(6, "张三6","北京",new Date()));userList.add(new User(7, "张三7","北京",new Date()));model.addAttribute("userList",userList);model.addAttribute("flag",false);//返回模板文件的名称即可return "hello";}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com