SpringBoot 配置 Thymeleaf模板引擎

Thymeleaf模板引擎

什么是模板引擎

模板引擎(这里特指用于Web开发的模板引擎)是为了使用户界面与业务数据(内容)分离而产生的,它可以生成特定格式的文档,用于网站的模板引擎就会生成一个标准的HTML文档。


学习视频: http://www.itlaoqi.com/chapter/1688.html

源码地址: QQ群 814077650 , 群共享中自助下载

老齐的官网: itlaoqi.com (更多干货就在其中)


Thymeleaf的特点

  • Thymeleaf优点
    主流唯一的前后端通用模板引擎,静态html嵌入标签属性,浏览器可以直接打开模板文件,便于前后端联调。
    springboot官方推荐方案。
  • Thymeleaf缺点
    模板必须符合xml规范。
    慢!

    老齐的建议

  • 虽然Thymeleaf是Spring Boot官方默认,但在中国太过小众。
  • 对于Thymeleaf,重点掌握模板引擎的理念,语法了解就行。
  • 找工作重点学习Freemarker,前瞻学习Beetl。
  • JSP忘了它吧,在”去J2EE”的大趋势下,谁用谁傻X。

    Thymeleaf环境搭建

    pom引入thymeleaf依赖

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

application.properties增加thymeleaf配置

不使用缓存,保证修改源文件后及时刷新

spring.thymeleaf.cache=false

在templates中创建标准index.html文件

thymeleaf模板必须定义th命名空间,其他均为标准HTML标签

<!DOCTYPE html>
<!--最重要的是要引入thymeleaf的命名空间 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    ...

在Controller准备测试数据,完成视图与模型绑定

@Controller
public class ThymeleafController {
    @RequestMapping("/")
    public ModelAndView index(String keyword) {
        //参数值index就对应了templates/index.html
        ModelAndView mav = new ModelAndView("index");
        ...
        //将查询结果放入mav
        mav.addObject("emps" , list);
        return mav;
    }
}

index.html使用th:each属性迭代emps集合

<tr th:each="emp,stat:${emps}" >
    <td>[[${emp.job}]]</td>
    <td>[[${#dates.format(emp.hiredate , 'yyyy年MM月dd日')}]]</td>
</tr>

原文地址:https://www.cnblogs.com/itlaoqi/p/11391751.html

时间: 2024-08-02 19:07:15

SpringBoot 配置 Thymeleaf模板引擎的相关文章

SpringBoot:2.SpringBoot整合Thymeleaf模板引擎渲染web视图

在Web开发过程中,Spring Boot可以通过@RestController来返回json数据,那如何渲染Web页面?Spring Boot提供了多种默认渲染html的模板引擎,主要有以下几种: Thymeleaf FreeMarker Velocity Groovy Mustache Spring Boot 推荐使用这些模板引擎来代替 Jsp,Thymeleaf 只是其中一种,下面我们来简单聊聊Thymeleaf及实践一下如何整合Spring Boot和Thymeleaf. 1.Thyme

springboot整合Thymeleaf模板引擎

引入依赖 需要引入Spring Boot的Thymeleaf启动器依赖. <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>nekohtml</groupId&g

SpringBoot使用thymeleaf模板引擎

(1).添加pom依赖 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-thymeleaf</artifactId> 4 </dependency> * SpringBoot1.x 默认的thymeleaf版本低,如果要自定义版本,需要在pom properties 覆写SpringBoot默认

SpringBoot使用thymeleaf模板引擎引起的模板视图解析错误

1 Whitelabel Error Page 2 This application has no explicit mapping for /error, so you are seeing this as a fallback. 3 4 Thu Sep 19 16:54:45 CST 2019 5 There was an unexpected error (type=Internal Server Error, status=500). 6 An error happened during

在springboot整合thymeleaf模板引擎中@Controller和@RestController不同注解的跳转页面方法

注:本文纯属学习记录,以备后续查阅! 1.通过@RestController注解实现页面跳转: 对应H5静态页面 2.使用@Controller注解实现页面跳转 对应H5静态页面: 之所以会出现@Controller和@RestController两种注解实现跳转页面不同的方式主要的原因是: 通过@RestController源码知道@RestController是@Controller和@ResponseBody的组合注解 如果需要跳转到指定 的页面,需要使用@Controller注解和视图解

SpringBoot入门篇--使用Thymeleaf模板引擎进行页面的渲染

在做WEB开发的时候,我们不可避免的就是在前端页面之间进行跳转,中间进行数据的查询等等操作.我们在使用SpringBoot之前包括我在内其实大部分都是用的是JSP页面,可以说使用的已经很熟悉.但是我们在使用springBoot开发框架以后我们会发现一个致命的问题,就是SpringBoot对Jsp的支持可以说是惨不忍睹,官方推荐我们进行使用的是Thymeleaf模板引擎进行.其实我这里也没搞清楚模板引擎原理是什么,以后有机会再深入了解,我们只需要学会怎么用就行,目前而言.当然模板引擎有很多,比如f

(二)SpringBoot基础篇- 静态资源的访问及Thymeleaf模板引擎的使用

一.描述 在应用系统开发的过程中,不可避免的需要使用静态资源(浏览器看的懂,他可以有变量,例:HTML页面,css样式文件,文本,属性文件,图片等): 并且SpringBoot内置了Thymeleaf模板引擎,可以使用模板引擎进行渲染处理,默认版本为2.1,可以重新定义Thymeleaf的版本号,在maven的配置文件中配置如下内容: <properties> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> &l

Spring Boot? 使用Thymeleaf模板引擎渲染web视图

静态资源访问 在我们开发Web应用的时候,需要引用大量的js.css.图片等静态资源. 默认配置 Spring Boot默认提供静态资源目录位置需置于classpath下,目录名需符合如下规则: /static /public /resources /META-INF/resources 举例:我们可以在src/main/resources/目录下创建static,在该位置放置一个图片文件.启动程序后,尝试访问http://localhost:8080/D.jpg.如能显示图片,配置成功. 渲染

Thymeleaf模板引擎的初步使用

在springboot中,推荐使用的模板引擎是Thymeleaf模板引擎,它提供了完美的Spring MVC的支持.下面就简单的介绍一下Thymeleaf模板引擎的使用. 在controller层中,使用在类上使用@controller注解,注意的是,这里不是@restController注解,因为@restController注解是将结果作为json字符串进行返回的,并不是调用模板. 方法中,注入参数,Model model.然后同springmvc中的modelandview一样,model