文章目录
thymeleaf 认识
Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。
相较与其他的模板引擎,它有如下三个极吸引人的特点:
1.Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
2.Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。
3.Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。
其实最主要是SpringBoot官方推荐thymeleaf
遇到问题
(1) This application has no explicit mapping for /error, so you are seeing this as a fallback。
原因是找不到正确的页面,可能文件放的位置有误。
更多原因情况请看 详情
(2) 网上说spring-boot-starter-thymeleaf 可以代替 spring-boot-starter-web,自己实践时,不管用,无法识别@RequestMapping()等注解,没办法,最终两个全写上。
核心pom.xml文件如下:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--添加此依赖用于浏览器解析html文件-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
</dependencies>
(3) 页面可以访问到,数据出不来,在后台可以打印出来,是自己ModelView和model 用的有误。
详细请看 Model、ModelAndView和 ModelMap的区别
详细代码如下:
@Controller
@RequestMapping("/learn")
public class PersonController {
@RequestMapping("/thymeleaf")
public String index(Model model) {
List<Person> learnList = new ArrayList<Person>();
Person bean = new Person("官方参考文档", "Spring Boot Reference Guide", "http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application");
learnList.add(bean);
bean = new Person("官方SpriongBoot例子", "官方SpriongBoot例子", "https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples");
learnList.add(bean);
bean = new Person("龙国学院", "Spring Boot 教程系列学习", "http://www.roncoo.com/article/detail/125488");
learnList.add(bean);
bean = new Person("嘟嘟MD独立博客", "Spring Boot干货系列 ", "http://tengj.top/");
learnList.add(bean);
bean = new Person("后端编程嘟", "Spring Boot教程和视频 ", "http://www.toutiao.com/m1559096720023553/");
learnList.add(bean);
bean = new Person("程序猿DD", "Spring Boot系列", "http://www.roncoo.com/article/detail/125488");
learnList.add(bean);
bean = new Person("纯洁的微笑", "Sping Boot系列文章", "http://www.ityouknow.com/spring-boot");
learnList.add(bean);
bean = new Person("CSDN——小当博客专栏", "Sping Boot学习", "http://blog.csdn.net/column/details/spring-boot.html");
learnList.add(bean);
bean = new Person("梁桂钊的博客", "Spring Boot 揭秘与实战", "http://blog.csdn.net/column/details/spring-boot.html");
learnList.add(bean);
bean = new Person("林祥纤博客系列", "从零开始学Spring Boot ", "http://412887952-qq-com.iteye.com/category/356333");
learnList.add(bean);
model.addAttribute("name","大涛");
model.addAttribute("learnList", learnList);
return "index";
}
@RequestMapping("/thymeleafs")
public ModelAndView indexs() {
List<Person> learnList = new ArrayList<Person>();
Person bean = new Person("官方参考文档", "Spring Boot Reference Guide", "http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/#getting-started-first-application");
learnList.add(bean);
bean = new Person("官方SpriongBoot例子", "官方SpriongBoot例子", "https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples");
learnList.add(bean);
bean = new Person("龙国学院", "Spring Boot 教程系列学习", "http://www.roncoo.com/article/detail/125488");
learnList.add(bean);
bean = new Person("嘟嘟MD独立博客", "Spring Boot干货系列 ", "http://tengj.top/");
learnList.add(bean);
bean = new Person("后端编程嘟", "Spring Boot教程和视频 ", "http://www.toutiao.com/m1559096720023553/");
learnList.add(bean);
bean = new Person("程序猿DD", "Spring Boot系列", "http://www.roncoo.com/article/detail/125488");
learnList.add(bean);
bean = new Person("纯洁的微笑", "Sping Boot系列文章", "http://www.ityouknow.com/spring-boot");
learnList.add(bean);
bean = new Person("CSDN——小当博客专栏", "Sping Boot学习", "http://blog.csdn.net/column/details/spring-boot.html");
learnList.add(bean);
bean = new Person("梁桂钊的博客", "Spring Boot 揭秘与实战", "http://blog.csdn.net/column/details/spring-boot.html");
learnList.add(bean);
bean = new Person("林祥纤博客系列", "从零开始学Spring Boot ", "http://412887952-qq-com.iteye.com/category/356333");
learnList.add(bean);
for(Person p :learnList){
System.out.println(p);
}
ModelAndView modelAndView = new ModelAndView("index");
modelAndView.addObject("learnList", learnList);
modelAndView.addObject("name","小涛");
return modelAndView;
}
}
具体代码 (上面有的不在重复)
整体结构图
实体类 Person
@Data
@AllArgsConstructor
public class Person {
private String author;
private String title;
private String url;
}
application.prpperties(具体用到的)
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
# 属性配置用于浏览器解析html文件
spring.thymeleaf.content-type=text/html
# 开发环境中关闭缓存便于测试
spring.thymeleaf.cache=false
spring.thymeleaf.mode =LEGACYHTML5
实现效果
application.prpperties (默认thymeleaf 的详细配置文件)
#THYMELEAF(ThymeleafAutoConfiguration)
#开启模板缓存(默认值:true)
spring.thymeleaf.cache = true
#Check that the template exists before rendering it.spring.thymeleaf.check - template = true
#检查模板位置是否正确(默认值: true)
spring.thymeleaf.check - template - location = true
#Content - Type的值(默认值:text / html)
spring.thymeleaf.content - type = text / html
#开启MVC Thymeleaf视图解析(默认值:true)
spring.thymeleaf.enabled = true
#模板编码
spring.thymeleaf.encoding = UTF - 8
#要被排除在解析之外的视图名称列表,用逗号分隔
spring.thymeleaf.excluded - view - names =
#要运用于模板之上的模板模式。另见StandardTemplate - ModeHandlers(默认值:HTML5)
spring.thymeleaf.mode = HTML5
#在构建URL时添加到视图名称前的前缀(默认值:classpath: /templates/)
spring.thymeleaf.prefix = classpath: /templates/
#在构建URL时添加到视图名称后的后缀(默认值:.html)
spring.thymeleaf.suffix = .html
#Thymeleaf模板解析器在解析器链中的顺序。默认情况下,它排第一位。顺序从1开始,只有在定义了额外的TemplateResolver Bean时才需要设置这个属性。spring.thymeleaf.template - resolver - order =
#可解析的视图名称列表,用逗号分隔
spring.thymeleaf.view - names =
参考文章
嘟嘟独立博客之thymeleaf的运用 案例来源此