最代码-泽正
2017-12-21 14:32:17
springboot整合Thymeleaf入门实例
写在开始:今天手头很闲,就写了springboot整合Thymeleaf的demo,以前的项目都是以jsp为模板来进行前后端开发,而springboot官方不建议使用jsp,官方推荐使用Thymeleaf模板引擎,以达到前后端分离的目的,方便前后端牛牛进行无缝开发,互不影响。
正文:首先我们从springboot官网下载demo,然后将下载下来的工程导入开发工具里,第一步:在pom文件里添加Thymeleaf依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
第二步在application.properties中添加配置:
# thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.check-template-location=true spring.thymeleaf.suffix=.html spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.mode=HTML5 spring.thymeleaf.cache=false
第三步在src/main/resources下的templates下新建一个html页面即可。
到此整个整合流程就结束了。
接下来我们写个controller和html测试一下。
controller:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>spring——Thymeleaf demo</title> </head> <body> <p th:text="'hello, ' + ${msg} + '!'" /> <h1 th:text="${msg}">hello</h1> </body> </html>
然后我们浏览器访问:http://localhost:8080/hello 得到结果如图:
访问http://localhost:8080/helloworld 得到结果如图:
这样整个demo就完成了。喜欢的牛牛可以参考!
写在最后:勤能补拙,厚积薄发!
评论

sharic1985 LV1
2023年4月23日
水光浮藻 LV6
2021年5月19日
随风的叶
2021年4月21日
暂无贡献等级
260670106
2020年9月9日
暂无贡献等级
yunlulu
2020年7月1日
暂无贡献等级
lzldzs163 LV1
2020年4月25日
litcwy LV3
2020年4月23日
zhaoming200677 LV13
2019年11月29日
qyn1013 LV1
2019年11月28日
rtw888 LV1
2019年11月26日