springboot怎么替代jsp_SpringBoot集成Thymeleaf

860d91a2c2f3b47094ee506ca9fef74c.png

前面章节我们介绍了SpringBoot集成jsp和Freemarker以及它们的具体应用。而在这些前端模板引擎中,SpringBoot首推使用Thymeleaf。这是因为Thymeleaf对SpringMVC提供了完美的支持。

Thymeleaf简介

Thymeleaf同样是一个Java类库,能够处理HTML/HTML5、XML、JavaScript、CSS,甚⾄纯⽂本。通常可以用作MVC中的View层,它可以完全替代JSP。

Thymeleaf的特性

  • Thymeleaf不仅可以作为模板存在,同时也支持HTML原型。通过在HTML标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释HTML时会忽略未定义的标签属性,所以可直接通过浏览器打开;当有数据返回到页面时,Thymeleaf标签会动态地替换掉静态内容,使页面动态显示。
  • Thymeleaf开箱即用的特性。它支持标准方言和Spring方言,可以直接套用模板实现JSTL、OGNL表达式效果,避免重复套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
  • Thymeleaf提供Spring标准方言和一个与SpringMVC完美集成的可选模块,可以快速地实现表单绑定、属性编辑器、国际化等功能。

与其他模板引擎相比,Thymeleaf不会破坏文档结构。对比Freemarker可以看出效果:

FreeMarker: 

${message}

Thymeleaf:

Hello World!

注意,由于Thymeleaf使用了XML DOM解析器,因此它并不适合于处理大规模的XML文件。

实例演示

SpringBoot中创建项目并集成Thymeleaf。创建过程中勾选对应集成框架。

e3d2f547699a70514e95f0913d561c3e.png

项目创建之后,pom中对应的核心依赖如下:

org.springframework.boot    spring-boot-starter-thymeleaforg.springframework.boot    spring-boot-starter-weborg.projectlombok    lombok    true

实体类Student:

@Datapublic class Student {    private String idNo;    private String name;}

Controller对应实现:

@Controllerpublic class StudentController {    @GetMapping("/")    public String getStudents(Model model){        List list = new ArrayList<>();        Student s1 = new Student();        s1.setIdNo("N01");        s1.setName("Tom");        list.add(s1);        Student s2 = new Student();        s2.setIdNo("N02");        s2.setName("David");        list.add(s2);        model.addAttribute("students",list);        model.addAttribute("hello","Hello Thymeleaf!");        return "student";    }}

在Controller中实现了两个参数的返回一个为字符串,一个为Student的列表。

对应templates下的页面为student.html。Thymeleaf默认使用html作为后缀。

student.html页面展示信息如下:

    Hello Thymeleaf

Hello World

其中第一个为直接展示字符串,第二个为遍历列表并展示其中的属性值。

访问对应请求http://localhost:8080/,即可返内容展示。

26b3a3df5fa139c5d256d08182605923.png

注意事项

如果是在开发环境中,最好在application.properties中添加配置:

spring.thymeleaf.cache=false

关闭Thymeleaf的缓存(默认为true),避免因缓存导致修改需重启才能生效,生产环境可采用默认值。

使用Thymeleaf的页面必须在HTML标签中作如下声明,表示使用Thymeleaf语法:

SpringBoot中相关配置

SpringBoot中提供了大量关于Thymeleaf的配置项目:

# 开启模板缓存(默认值:true)spring.thymeleaf.cache=true # 检查模板是否存在spring.thymeleaf.check-template=true # 检查模板位置是否正确(默认值:true)spring.thymeleaf.check-template-location=true# Content-Type的值(默认值:text/html)spring.thymeleaf.content-type=text/html# 开启MVC Thymeleaf视图解析(默认值:true)spring.thymeleaf.enabled=true# 模板编码spring.thymeleaf.encoding=UTF-8# 排除视图名称列表,用逗号分隔spring.thymeleaf.excluded-view-names=# 模板模式,设置为HTML5会严格校验,不符合规则将报错spring.thymeleaf.mode=HTML5# 视图名称前缀(默认值:classpath:/templates/)spring.thymeleaf.prefix=classpath:/templates/# 视图名称后缀(默认值:.html)spring.thymeleaf.suffix=.html# 可解析的视图名称列表,用逗号分隔spring.thymeleaf.view-names=

关于SpringBoot集成Thymeleaf的操作已经完成,非常简单。

本文首发来自微信公众号:程序新视界。一个软实力、硬技术同步学习的平台。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
## springboot整合thymeleaf ### 1. 导入起步依赖 ```xml org.springframework.boot spring-boot-starter-thymeleaf ``` ### 2. 更改引入版本 ```xml 3.0.2.RELEASE 2.1.1 ``` > 1. springboot自带的thymeleaf依赖为2.1.3版本,使用thymeleaf-layout-dialect版本为2以下版本。 > 2. 使用3或3以上的thymeleaf时,需要thymeleaf-layout-dialect的版本为2或以上。 > 3. 锁定thymeleaf版本时不能使用thymeleaf.version标签,会和springboot内部的依赖标签冲突。应当使用springboot-thymeleaf.version标签来锁定版本。 ### 3. 配置文件配置 ```properties spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML spring.thymeleaf.cache=false ``` > spring.thymeleaf.cache为缓存,需要热部署时,需要设置为false ## 语法 ### 1. 替换标签体内容 ```html 显示欢迎 显示欢迎 ``` ### 2. 替换属性 ```html 显示欢迎 ``` ### 3. 在表达式中访问属性域 ```html 访问属性域 访问请求域 方式一 访问请求域 方式二 访问Session域 访Session域 方式一 访问Application域 方式一 ``` ### 4. 解析url地址 ```html 解析URL地址,获取ContextPath的值 @{}是把ContextPath的值附加到指定的地址前 @{}是把ContextPath的值附加到指定的地址前 ``` ### 5. 直接执行表达式 ```html 直接执行表达式 无转义效果 : [[${attrRequestScope}]] 有转义效果 : [(${attrRequestScope})] ``` ### 6. 分支与迭代 #### 1. if 判断 ```html if判断字符串是否为空 <p th

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值