【spring boot 使用模板引擎】

spring boot 使用模板引擎
使用 thymeleaf 模板引擎
整体步骤:
(1)
在 pom.xml 中引入 thymeleaf;
(2)
如何关闭 thymeleaf 缓存
(3)
编写模板文件.html
Spring Boot 默认就是使用 thymeleaf 模板引擎的,所以只需要在 pom.xml 加入依赖即可:
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-thymeleaf </ artifactId >
</ dependency >
Thymeleaf 缓 存 在开发 过 程中, 肯 定 是不行的,那么就要在开发的时候 把 缓 存 关 闭 , 只 需要在
application.properties 进行配置即可:
关注公众号 Java 核⼼技术获取更多教程 26 / 108
########################################################
###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
<! 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.hpit.test.web. ThymeleafController):
import java.util.Map;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* TODO thymeleaf模板引擎控制器
* @author Administrator
*
*/
@Controller
public class ThymeleafController {
@RequestMapping ( "/helloHtml" )
public String hello(Map<String, Object> map ) {
map .put( "hello" , "this data is from backing server" );
return "helloHtml" ;
}
}
启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:

 

使用 freemarker 模板引擎
使用 freemarker 也很简单,
在 pom.xml 加入 freemarker 的依赖
< dependency >
< groupId > org.springframework.boot </ groupId >
< artifactId > spring-boot-starter-freemarker </ artifactId >
</ dependency >
剩下的编码部分都是一样的,说下 application.properties 文件:
########################################################
###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
开发 freemarker 模板
helloHtml1.ftl
<! DOCTYPE html>
< html >
< head >
< meta charset = "UTF-8" >
< title > freemarker模板的使用 </ title >
</ head >
< body >
< h1 > ${message} </ h1 >
</ body >
</ html >
开发控制器:
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* TODO freemarker 控制器
*
*
*/
@Controller
@RequestMapping ( "/freemarker" )
public class FreemarkerController {
@RequestMapping ( "/hello" )
public String hello(ModelMap map ) {
map .put( "message" , "this data is from backing server , for freemarker" );
return "helloHtml1" ;
}
}
访问地址: http://localhost:8080/freemarker/hello

 

thymeleaf 和 freemarker 是可以共存的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

God Zhang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值