简单说, Thymeleaf 是一个跟 Velocity、FreeMarker 类似的模板引擎,它可以完全替代 JSP 。
如果不配置,会自动应用默认设置
#thymelea模板配置 spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.content-type=text/html spring.thymeleaf.cache=false spring.resources.chain.strategy.content.enabled=true spring.resources.chain.strategy.content.paths=/**thymeleaf 是spring官方推荐的视图层技术,所以spring提供了thymeleaf的启动包
<!--thymeleaf 是spring官方推荐的视图层技术,所以spring提供了thymeleaf的启动包--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>在classpath:templates目录下,对应的maven映射目录:src/resoures/templates
下面html文件应该放在templates目录下
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <h1>hello world</h1> <h2 th:text="${msg}"></h2> </body> </html>App
package cn.liuhao; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }controller
package cn.liuhao.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("api") public class ApiController { @RequestMapping("index") public String index(Model model) { model.addAttribute("msg", "你好!现在你应该看到了这条信息"); return "index"; } }在thymeleaf中因为语法比较严谨,所以必须认定所有的html标签都必须有结束标记
在mate标签后加结束标签即可
更改thymeleaf包的版本,更改到更高版本即可解决这个问题 在pom.xml中加入以下代码
<properties> <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.4.RELEASE</thymeleaf-layout-dialect.version> </properties>如果为空,则返回true,否则返回false
<h1 th:text="${#strings.isEmpty(msg)}"></h1>dates常用方法
<!-- 不给格式,默认转换为当前时区格式 --> <h2 th:text="${#dates.format(date)}"></h2> <!-- 指定格式 --> <h2 th:text="${#dates.format(date,'yyyy/MM/dd')}"></h2> <!-- 获取年份 --> <h2 th:text="${#dates.year(date)}"></h2> <!-- 获取月份 --> <h2 th:text="${#dates.month(date)}"></h2> <!-- 获取日份 --> <h2 th:text="${#dates.day(date)}"></h2>controller控制器
@Controller public class UserController { @RequestMapping("showUsers") public String showUsers(Model model) { List<User> users = new ArrayList<User>(); users.add(new User(1, "张三", 99.9)); users.add(new User(2, "李四", 88.9)); users.add(new User(3, "王五", 77.9)); model.addAttribute("list", users); return "showUsers"; } }showUsers.html
<body> <table border="1px"> <thead> <tr> <th>#ID</th> <th>#name</th> <th>#score</th> </tr> </thead> <tbody> <tr th:each="item : ${list}"> <td th:text="${item.id}"></td> <td th:text="${item.name}"></td> <td th:text="${item.score}"></td> </tr> </tbody> </table> <table border="1px"> <thead> <tr> <th>#ID</th> <th>#name</th> <th>#score</th> <th>#当前索引</th> <th>#当前条数</th> <th>#齐数</th> <th>#偶数</th> <th>#集合大小</th> <th>#当前是否为第一条</th> <th>#当前是否为最后一条</th> </tr> </thead> <tbody> <tr th:each="item,var : ${list}"> <td th:text="${item.id}"></td> <td th:text="${item.name}"></td> <td th:text="${item.score}"></td> <td th:text="${var.index}"></td> <td th:text="${var.count}"></td> <td th:text="${var.even}"></td> <td th:text="${var.odd}"></td> <td th:text="${var.size}"></td> <td th:text="${var.first}"></td> <td th:text="${var.last}"></td> </tr> </tbody> </table> </body>controller控制器
@Controller public class UserController { @RequestMapping("showUsers") public String showUsers(Model model) { Map<String, Object> map = new HashMap<String, Object>(); map.put("use1", new User(1, "张三", 99.9)); map.put("use2", new User(2, "李四", 88.8)); map.put("use3", new User(3, "王五", 77.7)); model.addAttribute("map", map); return "showUsers"; } }showUsers.html
<body> <table border="1px" align="center"> <thead> <tr> <th>key</th> <th>value</th> <th>id</th> <th>name</th> <th>score</th> </tr> </thead> <tbody> <tr th:each="item : ${map}"> <td th:text="${item.key}"></td> <td th:text="${item.value}"></td> <td th:text="${item.value.id}"></td> <td th:text="${item.value.name}"></td> <td th:text="${item.value.score}"></td> </tr> </tbody> </table> </body>controller内容
@Controller public class UserController { @RequestMapping("showUsers") public String showUsers(HttpServletRequest request) { // 向request作用域存入数据 request.setAttribute("requestMsg", "this is a msg in request field"); // 向seesion作用域存入数据 request.getSession().setAttribute("sessionMsg", "this is a msg in session field"); // 向application作用域存入数据 request.getSession().getServletContext().setAttribute("applicationMsg", "this is a msg in application field"); return "showUsers"; } }showUsers.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <!--request作用域取数据 --> <h2 th:text="${#httpServletRequest.getAttribute('requestMsg')}"></h2> <!-- session作用域取数据 --> <h2 th:text="${session.sessionMsg}"></h2> <!-- applcation作用域取数据 --> <h2 th:text="${application.applicationMsg}"></h2> </body> </html>与普通语法不同,url需要用@{}包括起来
@{}