springboot2.x——thymeleaf引擎模板

  java的引擎模板主要有:thymeleaf、freemarker、volecity等等,有兴趣的可以去了解另外两个模板,此处只说thymeleaf。(三者的优点与缺点:https://blog.csdn.net/ztchun/article/details/76407612)

  thymeleaf是什么?

  • 1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
  • 2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  • 3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

  springboot为什么推荐使用thymeleaf?

  • 提供了完美的springmvc支持
  • thymeleaf既是原型又是页面,开发速度更快,符合springboot的理念。

  pom.xml引入thymeleaf依赖:

  

  nekohtml依赖:非严格的Html严格

  创建application.properties文件(此处,为了方便阅读,实际开发会使用yml文件)  

  yml语法需要注意一点:格式需要对齐,请勿使用tab键!!!!

spring:
  thymeleaf:
    cache: false # 开发时关闭缓存,不然没法看到实时页面
    mode: LEGACYHTML5 # 用非严格的 HTML
    encoding: UTF-8
    servlet:
      content-type: text/html

server:
  port: 9090 #更改tomcat端口

一个简单的测试用例:创建一个javaBean和对应的controller

User.java

 1 package com.baiye.springboothello.entity;
 2
 3 public class User {
 4     private Long id;
 5     private String userName;
 6     private int age;
 7
 8
 9     public User() {
10
11     }
12
13     public User(Long id, String userName, int age) {
14         this.id = id;
15         this.userName = userName;
16         this.age = age;
17     }
18
19     public Long getId() {
20         return id;
21     }
22
23     public void setId(Long id) {
24         this.id = id;
25     }
26
27     public String getUserName() {
28         return userName;
29     }
30
31     public void setUserName(String userName) {
32         this.userName = userName;
33     }
34
35     public int getAge() {
36         return age;
37     }
38
39     public void setAge(int age) {
40         this.age = age;
41     }
42 }

UserController.java

 1 package com.baiye.springboothello.controller;
 2
 3 import com.baiye.springboothello.entity.User;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.ui.Model;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RequestMethod;
 8
 9 import java.util.ArrayList;
10 import java.util.List;
11
12 @Controller
13 public class UserController {
14     @RequestMapping(value = "/getUserInfo",method = RequestMethod.GET)
15     public String getUserInfo(Model model){
16         User user = new User(100L,"admin",18);
17         User user2 = new User(101L,"李四",19);
18         User user3 = new User(102L,"张三",20);
19         User user4 = new User(103L,"王五",21);
20         List<User> list = new ArrayList<>();
21         list.add(user2);
22         list.add(user3);
23         list.add(user4);
24         model.addAttribute("user",user);
25         model.addAttribute("list",list);
26         return "userInfo";
27     }
28 }

对于前端的文件:html、css、js等静态文件,springboot推荐存存放于resources目录下的static中,但是这里我们使用了thymeleaf引擎模板,因此html应该放在resources下另一个目录中——template下:

userInfo.html

 1 <!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Hello Thymeleaf</title>
 6 </head>
 7 <body>
 8 <div>
 9     <span>访问 Model:</span><span th:text="${user.userName}"></span>
10 </div>
11 <div>
12     <span>访问列表</span>
13     <table>
14         <thead>
15         <tr>
16             <th>编号</th>
17             <th>姓名</th>
18             <th>年龄</th>
19         </tr>
20         </thead>
21         <tbody>
22         <tr th:each="item : ${list}">
23             <td th:text="${item.id}"></td>
24             <td th:text="${item.userName}"></td>
25             <td th:text="${item.age}"></td>
26         </tr>
27         </tbody>
28     </table>
29 </div>
30 </body>
31 </html>

所有的引擎模板都需要引入,themeleaf不例外,请在头部引入:<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">

themeleaf更多的标签学习:https://blog.csdn.net/u014042066/article/details/75614906

启动main函数,注意端口的改变,运行结果如下:

原文地址:https://www.cnblogs.com/baiye195/p/9220515.html

时间: 2024-08-14 17:00:24

springboot2.x——thymeleaf引擎模板的相关文章

SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法

我们在使用SpringBoot框架的时候在前面已经介绍了Thymelea引擎模板,因为SpringBoot对JSP惨不忍睹的支持.那我们在使用引擎模板对前端页面进行渲染能够返回的情况下我们怎么才能在静态的HTML页面上加载动态的数据呢?这时候我们就要介绍关于Thymeleaf的一些基本使用方法了. 首先和我们JSP页面中导入C字库是一样的,我们需要进行一定的配置: 如下图所示,在html标签上进行两段添加 文本显示:我在session中保存了一个key值为 user1 的用户对象,我想在页面直接

Thymeleaf 搜索模板引擎

1.Thymeleaf是什么? Thymeleaf是一种用于Web和独立环境的现代服务器端的Java模板引擎. Thymeleaf的主要目标是将优雅的自然模板带到开发工作流程中,并将HTML在浏览器中正确显示,并且可以作为静态原型,让开发团队能更容易地协作.Thymeleaf能够处理HTML,XML,JavaScript,CSS甚至纯文本. Thymeleaf使用Spring框架的模块,与许多常见的工具集成在一起,并且可以插入自己的功能,是现代HTML5 JVM Web开发的理想选择,尽管Thy

Spring Boot中Thymeleaf引擎动态刷新

在Spring 项目中使用thymeleaf引擎,可以很好地解决部署问题,不用再导出jsp文件.真正做到一个jar包即可发布.但Thymeleaf有个缺陷:必须重启工程才能刷新.其实把配置稍微改一下,就可以在开发的时候动态刷新. 首先是把cache关掉 spring.thymeleaf.cache=false 然后把spring.tyhmeleaf.prefix指向一个本地路径,windows下如: file:/d:\\work\\project\\resources\\templates\\这

SpringBoot中Thymeleaf创建模板

SpringBoot中Thymeleaf创建模板 学习视频: http://www.itlaoqi.com/chapter/1688.html 源码地址: QQ群 814077650 , 群共享中自助下载 老齐的官网: itlaoqi.com (更多干货就在其中) 定义模板格式,使用th:fragment定义模板片段 header_footer.html 一个模板中可以使用th:fragment定义多个片段 <body> <nav class="navbar navbar-de

springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据

springboot框架中集成thymeleaf引擎,使用form表单提交数据,debug结果后台获取不到数据 表单html: <form class="form-horizontal form-material" th:object="${user}" th:action="@{/user/updateOneUserInfo}" method="post"> <input type="hidden

SpringBoot2(thymeleaf模板jsp页面和jpa)

一.thymeleaf模板 1.在pom.xml中导入依赖 <!-- 添加thymeleaf模版的依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId&

RestTemplateConfig写法,用于配置Template引擎模板

代码: 1 import org.springframework.context.annotation.Bean; 2 import org.springframework.context.annotation.Configuration; 3 import org.springframework.http.client.ClientHttpRequestFactory; 4 import org.springframework.http.client.SimpleClientHttpReque

art-template引擎模板

art-template简介 artTemplate(后文简称aT)才是模板引擎,而TmodJS(后文简称TJ,曾用名atc)则是依赖于前者的一款模板预编译器.两者都是由腾讯开发.其实aT完全可以独立使用,而TJ存在的意义是提供了一个对模板进行预编译的环境(基于NodeJS和模块化).有了TJ,aT可以支持按目录的方式存储模板.以include的方式对指定目录中的模板进行调用(注意:这里的include和aT中的include不一样,后者只能引用同一页面中的不同模板标签).自动监控模板变化与自动

SpringBoot2 整合 JSP视图模板 整合 Ueditor富文本编辑器

一般涉及到后台管理系统,就少不了富文本编辑器,这个可以图文视频混排高级工具,笔者通过对比,发现目前市场上最好的三方库还当属百度的 ueditor 近年来 SpringBoot 框架可谓越来越火,可是笔者发现 ueditor 只提供了对于 JSP 的版本,网上能找到很多继承的案列,但是大部分都是修改源码,或者 自己去实现上传文件的接口这使得一些功能不能被使用或者需要花大量的事件去实现上传文件的方法,通过权衡,还是 springboot + jsp + ueditor 的方式最简单 虽然 jsp 不