springboot如何使用thymeleaf为模板

这篇文章给大家分享的是有关spring boot如何使用thymeleaf为模板的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

在衡水等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都做网站、成都网站制作、成都外贸网站建设 网站设计制作按需制作网站,公司网站建设,企业网站建设,品牌网站建设,全网整合营销推广,成都外贸网站建设公司,衡水网站建设费用合理。

前言

在开发过程中,使用模板引擎是很有必要的。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";
 }
}

伍、页面效果

spring boot如何使用thymeleaf为模板

陆、数据渲染

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

spring boot如何使用thymeleaf为模板

感谢各位的阅读!关于“spring boot如何使用thymeleaf为模板”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

本文题目:springboot如何使用thymeleaf为模板
网页网址:https://www.cdcxhl.com/article10/johsgo.html

成都网站建设公司_创新互联,为您提供动态网站企业网站制作网站导航微信公众号外贸网站建设网站策划

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

外贸网站制作