Thymeleaf是一种模板语言。什么是模板语言或模板引擎?常见的模板语言包括以下概念:数据(Data)、模板(Template)、模板引擎(TemplateEngine)和结果文档(ResultDocuments)。数据数据是信息的表现形式和载体,可以是符号、文字、数字、语音、图像、视频等。数据和信息密不可分,数据是信息的表现形式,信息是数据的内涵。数据本身没有任何意义,数据只有在影响实体行为时才成为信息。模板模板是一个蓝图,即与类型无关的类。编译器在使用模板时,会根据模板的实参实例化模板,得到类型相关的类。模板引擎模板引擎(这里特指用于Web开发的模板引擎)是为了将用户界面与业务数据(内容)分离而产生的。它可以生成特定格式的文件,网站使用的模板引擎会生成标准的HTML文件。结果文档特定格式的文档,例如生成标准HTML文档的网站模板引擎。模板语言的用途非常广泛,常见的用途如下:页面渲染文档生成代码生成“数据+模板=文本”的所有应用场景这里案例的目的自然是页面渲染。接下来在SpringBoot中集成Thymeleaf,实现一个完整的Web案例。1、运行chapter-2-spring-boot-quick-startchapter-2-spring-boot-quick-start项目使用内存数据库,不需要配置数据源。只需下载并运行。1、下载项目gitclone下载项目springboot-learning-example,项目地址见GitHub:https://github.com/JeffLi1993/springboot-learning-example,即:gitclonehttps://github.com/JeffLi1993/springboot-learning-example.git2。项目结构用IDEA打开项目,可以看到子项目chapter-2-spring-boot-quick-start,其目录如下:├──pom.xml└──src├──main│├──java││└──spring││└──boot││└──core││├──QuickStartApplication.java│├──domain│││├──User.java││└──UserRepository.java││├──服务││├──UserService.java│└──impl││└──UserServiceImpl.java│└└──web││└──UserController.java│└──资源│├──application.properties│├──static││├──css│└──default.css││└──images│└└──favicon.ico│└──templates│├──userForm.html│└──userList.html└──test└──java└──spring└──boot└──core├──QuickStartApplicationTests.java└──domain└──UserRepositoryTests.java对应目录:org.spring.springboot.controller——控制器层org.spring.springboot.dao——数据操作层DAOorg.spring.springboot.domain——实体类org.spring.springboot.service——业务逻辑层Application——应用启动类application.properties-应用程序配置文件模板将使用以下两个目录。static目录用于存放CSS、JS等资源文件。templates目录用于存储视图。3、编译运行项目在项目根目录下,运行maven命令编译:cdchapter-2-spring-boot-quick-startmvncleaninstall项目编译成功后,右键运行应用程序main函数启动类命名为QuickStartApplication.java,然后在浏览器中访问localhost:8080/users:用户列表页:用户编辑页:2.详解chapter-2-spring-boot-quick-start项目代码:1.pom.xmlThymeleaf依赖模板引擎,只需要在pom.xml中添加Thymeleaf组件依赖即可:starter-thymeleafThymeleaf是什么?Thymeleaf是一个适用于Web和独立环境的现代服务器端Java模板引擎。Thymeleaf的主要目标是为您的开发工作流带来优雅的自然模板——HTML可以在浏览器中正确显示,也可以作为静态原型工作,从而实现更强大的协作在开发团队中。Thymeleaf是新一代Java模板引擎,在Spring4后推荐整体的pom.xml配置如下:4.0.0spring.boot.corechapter-2-spring-boot-quick-start0.0。1-SNAPSHOTjarchapter-2-spring-boot-quick-start第二章快速启动案例org.springframework.bootspring-boot-starter-parent1.5.7.RELEASEUTF-8UTF-81.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-starter-data-jpacom.h2databaseh2runtimeorg.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-maven-plugin2.Spring中Thymeleaf依赖配置在Boot工程中添加Thymeleaf依赖启动其默认配置如果想要自定义配置,可以在application.properties中配置如下:spring.thymeleaf.cache=true#Enabletemplatecaching.spring.thymeleaf.check-template=true#Checkthatthetemplateexistsbeforerenderingit.spring.thymeleaf.check-template-location=true#Checkthatthetemplateslocationexists.spring.thymeleaf.enabled=true#EnableThymeleafviewresolutionforWebframeworks.spring.thymeleaf.encoding=UTF-8#Templatefilesencoding.spring.thymeleaf.excluded-view-names=#Comma-separatedlistofviewnamesthatshouldbeexcludedfromresolution.spring.thymeleaf.mode=HTML5#Templatemodetobeappliedtodetemplates.SeealsoStandardTemplates视图名称的逗号分隔列表spring.thymeleaf.prefix=classpath:/templates/#PrefixthatgetsprependedtoviewnameswhenbuildingaURL.spring.thymeleaf.reactive.max-chunk-size=#Maximumsizeofdatabuffersusedforwritingtotheresponse,inbytes.spring.thymeleaf.reactive.media-types=#viewtechnology.spring.thymeleaf.servlet.支持的媒体类型。content-type=text/html#Content-TypevaluewrittentoHTTPresponses.spring。thymeleaf.suffix=.html#SuffixthatgetsappendedtoviewnameswhenbuildingaURL.spring.thymeleaf.template-resolver-order=#Orderofthetemplateresolverinthechain.spring.thymeleaf.view-names=#Comma-separatedlistofviewnamesthatcanberesolved.3.Thymeleaf使用Controller将View指向用户控制层代码如下:@Controller@RequestMapping(value="/users")//通过这个配置,下面的映射都在/userspublicclassUserController{@AutowiredUserServiceuserService;//用户服务层/***获取用户列表*处理“/users”GET请求,用于获取用户列表*通过@RequestParam传递参数,进一步实现条件查询或分页查询*/@RequestMapping(method=RequestMethod.GET)publicStringgetUserList(ModelMapmap){map.addAttribute("userList",userService.findAll());return"userList";}/***显示创建用户表单**/@RequestMapping(value="/create",method=RequestMethod.GET)publicStringcreateUserForm(ModelMapmap){map.addAttribute("user",newUser());map.addAttribute("action","create");return"userForm";}/***CreateUser*ProcessPOof"/users"ST请求,用于获取用户列表*通过@ModelAttribute绑定参数,通过@RequestParam从页面传递参数*/@RequestMapping(value="/create",method=RequestMethod.POST)publicStringpostUser(@ModelAttributeUseruser){userService.insertByUser(user);return"redirect:/users/";}/***显示用户表单需要更新*处理"/users/{id}"的GET请求,通过URL中的id值*URL@PathVariable绑定参数中的id*/@RequestMapping(value="/update/{id}",method=RequestMethod.GET)publicStringgetUser(@PathVariableLongid,ModelMapmap){map.addAttribute("user",userService.findById(id));map.addAttribute("action","update");return"userForm";}/***处理"/users/{id}"的PUT请求更新User信息**/@RequestMapping(value="/update",method=RequestMethod.POST)publicStringputUser(@ModelAttributeUseruser){userService.update(user);return"redirect:/users/";}/***processing"/用户/{id}"GET请求删除用户信息*/@RequestMapping(value="/delete/{id}",method=RequestMethod.GET)publicStringdeleteUser(@PathVariableLongid){userService.delete(id);return"redirect:/users/";}}ModelMap对象数据绑定到视图返回字符串,该字符串对应的目录为resources/templates名下的模板。@ModelAttribute注解用于获取页面Form表单提交的数据,绑定到User数据对象上。表格单页核心代码:名称