SpringBoot之模板引擎 |
您所在的位置:网站首页 › 后端模板引擎和前端框架 › SpringBoot之模板引擎 |
1. 什么是模板引擎
将模板文件和数据通过模板引擎生成一个HTML代码 jsp就是一个模板引擎,还有用的比较多的freemarker,包括SpringBoot给我们推荐的Thymeleaf。 SpringBoot这个项目首先是以jar的方式,不是war,而且还是用嵌入式的Tomcat,所以呢,他现在默认是不支持jsp的。 3. 常见的模板引擎JSP Thymeleaf Velocity Freemarker 模板引擎比较:https://blog.csdn.net/az44yao/article/details/99887733 4. 引入ThymeleafThymeleaf 官网:https://www.thymeleaf.org/ Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf Spring官方文档:找到我们对应的版本 https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter 导入依赖 org.springframework.boot spring-boot-starter-thymeleaf 编写controller类 @Controller public class HelloController { @GetMapping("/test") public String test(Model model) { model.addAttribute("msg","Hello SpringBoot"); return "test"; } } 编写在resources下的templates建立test.html注意: 使用前需引入约束 所有的htmL元素都可以被thymeLeaf替换接管: th:元素名 Title 测试 在浏览器输入:http://localhost:8080/test![]() 测试: 两种方式:推荐使用第一种 [[${user}]]测试: 参考内容:https://www.bilibili.com/video/BV1PE411i7CV 仅用于学习! |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |