
一、前言Spring Boot支持FreeMarker、Groovy、Thymeleaf和Mustache四种模板解析引擎官方推荐使用Thymeleaf。二、spring-boot-starter-thymeleaf在Spring Boot中使用Thymeleaf只需在pom中加入Thymeleaf的starter即可dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency在Spring Boot中默认的html页面地址为src/main/resources/templates默认的静态资源地址为src/main/resources/static。三、Thymeleaf默认配置在Spring Boot配置文件中可对Thymeleaf的默认配置进行修改spring: thymeleaf: cache: false #开启模板缓存默认值true mode: HTML5 #要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值HTML5) encoding: UTF-8 #模板编码 prefix: classpath:/templates/ #在构建URL时添加到视图名称前的前缀默认值classpath:/templates/多个位置用逗号隔开 suffix: .html #在构建URL时添加到视图名称后的后缀默认值.html一般开发中将spring.thymeleaf.cache设置为false其他保持默认值即可。四、简单示例编写一个简单的ControllerControllerpublicclassIndexController{RequestMapping(/account)publicStringindex(Modelm){ListAccountlistnewArrayListAccount();list.add(newAccount(1,KangKang,康康,e10adc3949ba59abbe56e,超级管理员,17777777777));list.add(newAccount(2,Mike,麦克,e10adc3949ba59abbe56e,管理员,13444444444));list.add(newAccount(3,Jane,简,e10adc3949ba59abbe56e,运维人员,18666666666));list.add(newAccount(4,Maria,玛利亚,e10adc3949ba59abbe56e,清算人员,19999999999));m.addAttribute(accountList,list);returnaccount;}}编写account.html页面!DOCTYPEhtmlhtmlxmlns:thhttp://www.thymeleaf.orgheadtitleaccount/titlemetahttp-equivContent-Typecontenttext/html; charsetUTF-8/linkrelstylesheetth:href{/css/style.css}typetext/css/headbodytabletrthno/ththaccount/ththname/ththpassword/ththaccountType/ththtel/th/trtrth:eachlist,stat : ${accountList}tdth:text${stat.count}/tdtdth:text${list.account}/tdtdth:text${list.name}/tdtdth:text${list.password}/tdtdth:text${list.accountType}/tdtdth:text${list.tel}/td/tr/table/body编写style.css页面table{margin:20px 40px 20px 0px;width:100%;border-collapse:collapse;border-spacing:0;table-layout:automatic;word-wrap:break-all}tabletbodytr:nth-of-type(odd){background-color:#F7F7F7}th, td{padding:8px;text-align:left;vertical-align:middle;font-weight:normal;font-size:12px;border-bottom:1px solid #fff;}th{padding-bottom:10px;color:#fff;font-weight:700;background:rgba(66,100,131,.9)}td{border-bottom-width:1px}最终项目目录如下所示启动项目访问http://localhost:8080/web/account