spring boot使用thymeleaf模板配置和freem
整体步骤:
(1) 在pom.xml中引入thymeleaf;
(2) 如何关闭thymeleaf缓存
(3) 编写模板文件.html
Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:
1
2
3
4
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-thymeleaf</ artifactId >
</ dependency >
|
Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在application.properties进行配置即可:
1
2
3
4
5
6
7
8
9
10
11
|
########################################################
###THYMELEAF (ThymeleafAutoConfiguration)
########################################################
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
#spring.thymeleaf.mode=HTML5
#spring.thymeleaf.encoding=UTF- 8
# ;charset=<encoding> is added
#spring.thymeleaf.content-type=text/html
# set to false for hot refresh
spring.thymeleaf.cache= false
|
编写模板文件src/main/resouces/templates/helloHtml.html
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE html>
< html xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" >
< head >
< title >Hello World!</ title >
</ head >
< body >
< h1 th:inline = "text" >Hello.v.2</ h1 >
< p th:text = "${hello}" ></ p >
</ body >
</ html >
|
编写访问路径(com.kfit.test.web.TemplateController):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package com.kfit.test.web;
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 模板测试.
* @author Administrator
*
*/
@Controller
publicclass TemplateController {
/**
* 返回html模板.
*/
@RequestMapping ( "/helloHtml" )
public String helloHtml(Map<String,Object> map){
map.put( "hello" , "from TemplateController.helloHtml" );
return "/helloHtml" ;
}
}
|
启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:
1
2
|
Hello.v.2
from TemplateController.helloHtml
|
使用freemarker
使用freemarker也很简单,
在pom.xml加入freemarker的依赖:
1
2
3
4
|
< dependency >
< groupId >org.springframework.boot</ groupId >
< artifactId >spring-boot-starter-freemarker</ artifactId >
</ dependency >
|
剩下的编码部分都是一样的,说下application.properties文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
########################################################
###FREEMARKER (FreeMarkerAutoConfiguration)
########################################################
spring.freemarker.allow-request-override=false
spring.freemarker.cache=true
spring.freemarker.check-template-location=true
spring.freemarker.charset=UTF-8
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=false
#spring.freemarker.prefix=
#spring.freemarker.request-context-attribute=
#spring.freemarker.settings.*=
#spring.freemarker.suffix=.ftl
#spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist
#spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved
|
com.kfit.test.web.TemplateController:
1
2
3
4
5
6
7
8
|
/**
* 返回html模板.
*/
@RequestMapping ( "/helloFtl" )
public String helloFtl(Map<String,Object> map){
map.put( "hello" , "from TemplateController.helloFtl" );
return "/helloFtl" ;
}
|
src/main/resouces/templates/helloFtl.ftl
1
2
3
4
5
6
7
8
9
10
11
|
<!DOCTYPE html>
< html xmlns = "http://www.w3.org/1999/xhtml" xmlns:th = "http://www.thymeleaf.org"
xmlns:sec = "http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" >
< head >
< title >Hello World!</ title >
</ head >
< body >
< h1 >Hello.v.2</ h1 >
< p >${hello}</ p >
</ body >
</ html >
|
访问地址:http://127.0.0.1:8080/helloFtl
1
2
|
Hello.v.2
from TemplateController.helloFtl
|
spring boot使用thymeleaf模板配置和freem相关推荐
- Spring Boot整合Thymeleaf模板引擎
转载自 Spring Boot整合Thymeleaf模板引擎 什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMa ...
- Spring Boot集成Thymeleaf模板引擎
一.Thymeleaf 模板介绍 Spring Boot 推荐使用Thymeleaf 来代替传统开发中的JSP,那么什么是Thymeleaf 模板引擎呢?下面就来简单的介绍一下. Thymeleaf ...
- js文件中怎么使用thymeleaf标签_007、Spring Boot集成Thymeleaf模板引擎
1. Thymeleaf 介绍 Thymeleaf 是适用于 Web 和独立环境的现代服务器端 Java 模板引擎. Thymeleaf 的主要目标是为您的开发工作流程带来优雅的自然模板 - 可以在浏 ...
- java 模板引擎_极简 Spring Boot 整合 Thymeleaf 页面模板
点击"牧码小子"关注,和众多大牛一起成长! 关注后,后台回复 java ,领取松哥为你精心准备的技术干货! 虽然现在慢慢在流行前后端分离开发,但是据松哥所了解到的,还是有一些公司在 ...
- 没有找到 html 模板,Spring Boot and Thymeleaf:找不到HTML模板
我正在嘗試用Thymeleaf創建一個基於Spring Boot的應用程序.我使用PetClinic樣本作爲起點. 我的應用程序找不到一些模板.Spring Boot and Thymeleaf:找不 ...
- Spring Boot和Thymeleaf:重新加载模板和静态资源,而无需重新启动应用程序
Thymeleaf是围绕自然模板的概念设计的,该模板允许进行静态原型制作:模板逻辑不会影响用作原型的模板. 尽管这是一项很棒的技术,但您可能还希望在运行的Spring Boot应用程序中查看结果,而不 ...
- Spring Boot 引入 Thymeleaf 及入门使用
目录: 引言 1.Spring Boot 引入 Thymeleaf 1.1 修改 Thymeleaf 版本 1.2 修改 Thymeleaf Layout Dialect 版本 2.Thymeleaf ...
- Spring Security 实战:Spring Boot 下的自动配置
点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 公众号「码农小胖哥」 1. 前言 我们在前几篇 ...
- Spring Boot结合thymeleaf
之前在Eclipse里写了个Spring Boot响应jsp的小demo,后来发现打成jar包导出之后找不到jsp文件了.经过在网上查阅信息与资料,发现Spring Boot对于jsp的支持其实是不好 ...
最新文章
- Mapping Persistence Classes 笔记2---ValueType的形式和映射
- 【粉丝福利放送】Python最新书籍免费领取!
- 1054 The Dominant Color (20 分)【难度: 简单 / 知识点: 思维】
- python语言是非跨平台语言吗_python是跨平台的语言吗
- pycharm安装javascript插件_IDEA必备插件系列-Rainbow
- emmet 工具的基本使用,总结
- 超实用!Google AutoML 图像分类模型全在这!
- 三天学会HTML5 ——多媒体元素的使用
- bootdo框架介绍使用
- Word VBA设置所有图表宽度并居中
- 【NanoPi T2】 8.uboot gmac网卡驱动(4) - 移植rtl8211e网卡驱动(首发)
- UVALive 6437 Power Plant 【最小生成树 + 思维】
- 使用Certbot配置SSL证书【ubuntu系统】
- c语言如何表示大于小于等于,Excel 公式中大于和小于等于计算应怎么写
- React-Native 知识点小结
- 通用权限管理设计篇(一)
- html2Canvas 边框虚线
- 新买了个redmi红米ax6s网速超快刷openwrt
- matlab修改坐标轴刻度间隔大小
- 交易员期货行情分析:持续震荡,等待机会