一、前言

常用的模板引擎有:JSP、Velocity、Freemarker、Thymeleaf

但是,Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎。而且,语法更简单,功能更强大,所以这里介绍一下springboot使用Thymeleaf的实例以及遇到的问题。

二、配置application.properties文件

#thymelea模板配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

spring.thymeleaf.prefix:设置模板的位置
spring.thymeleaf.suffix:设置模板后缀

说明一下,这些配置不是必须的,如果配置了会覆盖默认的。

在开发时建议将spring.thymeleaf.cache设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
比如你修改了一个文件,已经update到tomcat,但刷新页面还是之前的页面,就是因为缓存引起的。

三、在pom.xml中添加thymeleaf的依赖

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

四、controller测试

/*** @author 欧阳思海* @date 2018/7/25 9:42*/
@Controller
public class HelloController {@RequestMapping(value = "/test")public ModelAndView test(ModelAndView mv) {mv.setViewName("/test");mv.addObject("title","欢迎使用Thymeleaf!");return mv;}
}

五、编写thymeleaf模板页面

注意:
spring-boot项目静态文件目录:/src/java/resources/static
spring-boot项目模板文件目录:/src/java/resources/templates
所以test.html文件在/src/java/resources/templates下。

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" >
<head><title>Getting Started: Serving Web Content</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${title}" /><br/></body>
</html>

六、运行效果

在浏览器输入http://localhost:8080/test

这个只是thymeleaf模板的入门程序,更多的看下篇文章,具体讲解thymeleaf模板的语法。

玩转springboot:thymeleaf模板引擎入门程序相关推荐

  1. Thymeleaf模板引擎---SpringBoot

    Thymeleaf模板引擎 前端交给我们的页面,是html页面.如果是我们以前开发,我们需要把他们转成jsp页面,jsp好处就是当我们查出一些数据转发到JSP页面以后,我们可以用jsp轻松实现数据的显 ...

  2. springboot使用thymeleaf模板引擎时出现org.xml.sax.SAXParseException的原因与解决办法

    异常描述: 在springboot程序当中,使用thymeleaf作为视图的时候,跳转到页面上的时候,会出现org.xml.sax.SAXParseException的异常(SAX解析器解析xml文件 ...

  3. 九、SpringBoot集成Thymeleaf模板引擎

    Thymeleaf咋读!??? 呵呵,是不是一脸懵逼...哥用我的大学四级英文知识告诉你吧:[θaimlif]. 啥玩意?不会音标?...那你就这样叫它吧:"赛母李府",大部分中国 ...

  4. java 模板引擎_SpringBoot入门系列(四)如何整合Thymeleaf模板引擎

    前面介绍了Spring Boot的优点,然后介绍了如何快速创建Spring Boot 项目.不清楚的朋友可以看看之前的文章:https://www.cnblogs.com/zhangweizhong/ ...

  5. SpringBoot中模板引擎thymeleaf

    首先我们用SpringBoot创建一个支持thymeleaf的web项目 添加web支持 添加thymeleaf模板引擎 创建好该项目之后,在templates目录下创建一个普通的html文件,这个时 ...

  6. <12>springboot集成thymeleaf模板引擎

    创建一个springboot工程,导入以下依赖 <dependencies><!--springboot框架web组件依赖--><dependency><gr ...

  7. 【Springboot】SpringBoot基础知识及整合Thymeleaf模板引擎

    文章目录 SpringBoot简介 SpringBoot是什么 为什么要学习SpringBoot SpringBoot的优势 学习SpringBoot前要具备的基础 创建第一个SpringBoot项目 ...

  8. SpringBoot整合Thymeleaf模板引擎以及静态资源的访问

    SpringBoot整合Thymeleaf模板引擎静态资源访问的配置 Thymeleaf是一个现代服务器端Java模板引擎,适用于Web和独立环境,能够处理HTML,XML,JavaScript,CS ...

  9. 【SpringBoot】3、SpringBoot中整合Thymeleaf模板引擎

    SpringBoot 为我们提供了 Thymeleaf 自动化配置解决方案,所以我们在 SpringBoot 中使用 Thymeleaf 非常方便 一.简介 Thymeleaf是一个流行的模板引擎,该 ...

最新文章

  1. java string()函数_从Java中的String函数返回String构建器?
  2. 动态配置页面 之 组件系统
  3. APUS 朱辉:移动应用出海的大数据建设挑战
  4. 6.5 开始进入设计 … Transition to Design
  5. oracle安装要多久,一次耗时4天的oracle安装过程
  6. python报错cannot import name ‘BeautifulSoup‘ from ‘bs4‘
  7. 【Express】—get请求参数 restful API
  8. 敏捷开发中的MoSCoW优先级排序方法
  9. python自动化办公真的好用吗-Python做什么更合适?|老男孩Python自动化运维
  10. Python小白的数学建模课-B4. 新冠疫情 SIR模型
  11. 【react学习笔记】为什么页面只展示空标签
  12. 项目经理必须学会的财务知识
  13. Java OCR 图像智能字符识别技术,可识别中文
  14. android studio中的模拟器,使用Android Studio创建Andorid模拟器
  15. 计算机专业毕业设计致谢,计算机毕业论文致谢范文3篇
  16. 11.8号软基2.2.2
  17. 网页文档下载不了怎么办 亲测可用!建议收藏!
  18. 分布式消息队列kafka
  19. UVALive - 3055 Symmetric Order【水题】【STL】
  20. music_悲催的键盘手

热门文章

  1. linq to sql 插入值,以及如何取回自增的ID
  2. STM32之option bytes踩坑记录
  3. JTAG之IO口作为普通IO口使用时注意事项
  4. wasm逆向——(极客大挑战2021wasm
  5. Kubernetes CRD开发汇总
  6. 数据结构--环形链表
  7. HTML5中各种标签总结(列表标签、表格标签)
  8. python自动化—web页面操作之窗口切换
  9. django_rest_framework之GenericAPIView(二)之搜索、排序、分页讲解
  10. 官网拉取fuchsia代码