前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >springboot使用thymeleaf为模板

springboot使用thymeleaf为模板

作者头像
小尘哥
发布2018-08-15 10:44:16
4620
发布2018-08-15 10:44:16
举报
文章被收录于专栏:小尘哥的专栏小尘哥的专栏

jsp已经明显跟不上时代发展了,freemarker用的够够的?换thymeleaf试试吧。springboot官方推荐的是freemarker和thymeleaf,而thymeleaf相对于freemarker更让人感觉强大的,是他可以动态替换标签内静态内容,这样前端可以安心写页面,后台可以安心撸接口,只需要把变量替换一下即可,这种理念,不知道是VUE抄袭了thymeleaf还是thymeleaf抄袭了VUE,不过无所谓了 ,对于我们广大码奴来说,实用就好

01

pom引入

代码语言:javascript
复制
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

02

application.properties添加thymeleaf配置

spring.thymeleaf.cache=false spring.thymeleaf.check-template=true spring.thymeleaf.check-template-location=true spring.thymeleaf.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=utf-8 spring.thymeleaf.mode=HTML5 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html

03

编写html

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
                xmlns:th="http://www.thymeleaf.org">
<head>
    <title>demo</title>
</head>
<body>
<p>这是第一段</p>
<p th:text="${textValue}">这是第二段</p>
</body>
</html>

04

测试类

@RequestMapping("index") public String index(Model model){ String textValue = "上士闻道,仅能行之;中士闻道,若存若亡;下士闻道,大笑之。" + "不笑不足以为道。" + "故建言有之:明道若昧;进道若退;夷道若颣(lei);上德若谷,大白若辱,广德若不足,建德若偷,质真若渝;大方无隅;大器免成;大音希声;大象无形。" + "道隐无名。" + "夫唯道,善始且善成。"; model.addAttribute("textValue",textValue); return "demo/demo"; }

05

页面效果

6

VUE有个SSR(服务端渲染)的问题比较头疼,虽然也有解决方案(见我之前写的文章《前后端分离Nuxt.js解决SEO问题》),但总觉得还是让适合的技术做时候的业务比较好,而thymeleaf还是相当于在服务端渲染,查看页面源码如下:

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-01-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 陌与尘埃 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档