thymeleaf java8_8、模板引擎thymeleaf(百里香叶)

模板引擎有:JSP、Velocity、Freemarker、Thymeleaf  等.....

514587c88ef655287ee06a680f2f167f.png

对于模板引擎,SpringBoot(springboot使用的是嵌入式的tomcat所以不支持jsp)推荐的Thymeleaf;

1)、引入thymeleaf  (thyme:百里香,leaf:叶子  thymeleaf:百里香叶)

pom文件中导入依赖坐标(即开启springboot中的thymeleaf启动器):

org.springframework.boot

spring-boot-starter-thymeleaf

25e185eb4e09aa45bc2586999a55a756.png

切换其版本操作:在pom文件中的的properties标签中指定版本

48304ba5e6f9fe08f3fa1abda7d326ab.png

3.0.9.RELEASE

2.2.2

48304ba5e6f9fe08f3fa1abda7d326ab.png

2)、thymeleaf的使用

aa35a1eadd3903b614901279e2249560.png

d6fc3e58015abcdbed6c73a645e8a362.png

6394cfbf399ebb11b42a57f0a64002d3.png

使用规则:

ThymeleafProperties.java

48304ba5e6f9fe08f3fa1abda7d326ab.png

@ConfigurationProperties(

prefix = "spring.thymeleaf"

)

public class ThymeleafProperties {

private static final Charset DEFAULT_ENCODING;

public static final String DEFAULT_PREFIX = "classpath:/templates/";

public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;

private boolean checkTemplateLocation = true;

private String prefix = "classpath:/templates/";

private String suffix = ".html";

48304ba5e6f9fe08f3fa1abda7d326ab.png

只要我们把HTML页面放在classpath:/templates/,thymeleaf就能自动渲染;

测试:

48304ba5e6f9fe08f3fa1abda7d326ab.png

@Controller

public class TestController {

@RequestMapping("/test")

public String testThymelea(){

//classpath:/templates/th.html

return "th";

}

}

48304ba5e6f9fe08f3fa1abda7d326ab.png

静态文件

1d62c0b905a9564424d44791539e7d2c.png

测试结果:

439570f9a43fd244de05e067c6f8a8eb.png

3)、使用案例:

编写一个mapper设置hello的值为你好;然后跳转到success页面

@RequestMapping("/success")public String success(Mapmap){//使用thymeleaf模板引擎,自动找thymeleaf会找template下的页面进行渲染,

map.put("hello","你好!");return "success";

}

success.html;在标签中导入thymeleaf的命名空间,有提示作用

成功

成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值