springboot使用thymeleaf为模板

简介: jsp已经明显跟不上时代发展了,freemarker用的够够的?换thymeleaf试试吧。springboot官方推荐的是freemarker和thymeleaf,而thymeleaf相对于freemarker更让人感觉强大的,是他可以动态替换标签...

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

壹、pom引入

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

贰、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

叁、编写html

<!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>

肆、测试类

package com.mos.easyboot.admin.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("demo")
public class DemoController {


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

伍、页面效果

img_367cbb4965aa1e41b27b12fb17ca1528.png
4.png

陆、数据渲染

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

img_f393cf05b4f6c577cb8bc3c0b6bc2cff.png
5.png

目录
相关文章
|
前端开发 Java
SpringBoot下载xlsx模板,导出excel数据
SpringBoot下载xlsx模板,导出excel数据
1389 0
|
Java API Spring
Java SpringBoot 公众号集成模板推送消息
Java SpringBoot 公众号集成模板推送消息
|
Java Maven
springboot项目--freemarker使用ftl模板文件动态生成图片
springboot项目--freemarker使用ftl模板文件动态生成图片
1974 0
|
前端开发 Java
SpringBoot-6-模板Thymeleaf常用标签
SpringBoot-6-模板Thymeleaf常用标签
402 0
SpringBoot-6-模板Thymeleaf常用标签
|
存储 JavaScript 前端开发
【开发模板】Vue和SpringBoot的前后端分离开发模板(二)
【开发模板】Vue和SpringBoot的前后端分离开发模板
466 0
|
算法 Java
制作SpringBoot工程模板
制作SpringBoot工程模板
335 0
|
Java Maven 数据库
Springboot实现微信公众号模板消息发送
最近看了一个地推的公众号,每天都会给你推送好几条地推需要的模板消息,好一段时间没有做公众号开发了,最近刚申请了个服务号,刚好可以拿来开发。模板消息需要服务号才可以,申请服务号的话需要企业营业执照,个人的话是没有办法申请的。下面来分享一下我的开发过程。
1384 0
|
移动开发 运维 前端开发
SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
SpringBoot 整合 Thymeleaf & 如何使用后台模板快速搭建项目
668 0
|
Oracle Java 关系型数据库
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
Generator【SpringBoot集成】代码生成+knife4j接口文档(2种模板设置、逻辑删除、字段填充 含代码粘贴可用)保姆级教程(注意事项+建表SQL+代码生成类封装+测试类)
715 0