Springboot+Thymeleaf配置与使用
来源:互联网 发布:linux java home 配置 编辑:程序博客网 时间:2024/06/07 10:12
Springboot+Thymeleaf配置与使用
前言
Springboot默认是不支持JSP的,默认使用thymeleaf模板引擎。所以这里介绍一下springboot使用Thymeleaf的实例以及遇到的问题。
配置与使用
1.在application.properties文件中增加Thymeleaf模板的配置。
#thymelea模板配置spring.thymeleaf.prefix=classpath:/templates/spring.thymeleaf.suffix=.htmlspring.thymeleaf.mode=HTML5spring.thymeleaf.encoding=UTF-8spring.thymeleaf.content-type=text/htmlspring.thymeleaf.cache=falsespring.resources.chain.strategy.content.enabled=truespring.resources.chain.strategy.content.paths=/**
说明一下,这些配置不是必须的,如果配置了会覆盖默认的。
在开发时建议将spring.thymeleaf.cache设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
比如你修改了一个文件,已经update到tomcat,但刷新页面还是之前的页面,就是因为缓存引起的。
2.在pom.xml中添加thymeleaf的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
3.编写一个测试的Controller
@RequestMapping(value = "/greeting")public ModelAndView test(ModelAndView mv) { mv.setViewName("/greeting"); mv.addObject("title","欢迎使用Thymeleaf!"); return mv;}
可以看到,Controller与普通的SpringMVC的Controller无异。
4.编写greeting.html
spring-boot项目静态文件目录:/src/java/resources/static
spring-boot项目模板文件目录:/src/java/resources/templates
所以greeting.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" /> <link th:href="@{/css/1.css}" rel="stylesheet"/></head><body><p th:text="'Hello, ' + ${title}" /><br/><script th:src="@{/js/jquery/1.11.0/jquery.js}"></script><script> $(function(){ alert("page load finish."); });</script></body></html>
5.运行效果
这里这是一个基本的使用示例,更多Thymeleaf的用法请自行搜索。
阅读全文
0 0
- Springboot+Thymeleaf配置与使用
- SpringBoot入门-14(springboot配置thymeleaf使用YML)
- SpringBoot与Thymeleaf整合
- SpringBoot使用thymeleaf
- SpringBoot入门-9(springboot配置thymeleaf模板)
- springBoot thymeleaf与Spring整合
- SpringBoot与Thymeleaf实现国际化
- springboot(四):thymeleaf使用详解
- SpringBoot使用thymeleaf模板引擎
- springboot使用thymeleaf 解析异常
- SpringBoot (四) :thymeleaf 使用详解
- springboot(四):thymeleaf使用详解
- springboot(四):thymeleaf使用详解
- springboot中使用thymeleaf模板
- springboot(四):thymeleaf使用详解
- SpringBoot -- thymeleaf
- springboot-thymeleaf
- SpringBoot-thymeleaf
- php 做分页ajax页面不刷新
- 【小技巧】程序运行结束后弹窗提醒
- 根据下拉展示不同的表格内容
- IEEE Spectrum 2017 编程语言排行:Python 夺冠
- shiro内置过滤器研究
- Springboot+Thymeleaf配置与使用
- 微信小程序开发笔记
- 树的建立和遍历
- ubuntu 安装ImageMagic
- Python实现——CAN报文转换工具
- php使用pdo数据库连接失败
- Cinder学习--关于存储的相关概念
- 固定在顶部的进度条
- 2017 Multi-University Training Contest