物联网架构成长之路(14)-SpringBoot整合thymeleaf

  使用thymeleaf作为模版进行测试
  在pom.xml 增加依赖

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-thymeleaf</artifactId>
4 </dependency>

  在application.properties中进行配置

1 #thymeleaf start
2 spring.thymeleaf.mode=HTML5
3 spring.thymeleaf.encoding=UTF-8
4 spring.thymeleaf.content-type=text/html
5 #开发时关闭缓存,不然没法看到实时页面
6 spring.thymeleaf.cache=false
7 spring.thymeleaf.prefix=classpath:/templates/
8 spring.thymeleaf.suffix=.html
9 #thymeleaf end

  在 src/main/resources 下新建 templates 目录 并创建 hellohtml.html 文件

 1 <!DOCTYPE html>  
 2 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"  
 3       xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">  
 4     <head>  
 5         <title>Hello World!</title>  
 6     </head>  
 7     <body>  
 8         <h1 th:inline="text">Hello.v.2</h1>  
 9         <p th:text="${hello}"></p>  
10     </body>  
11 </html> 

  增加Controller

1 @Controller
2 @RequestMapping("/html")
3 public class ThymeleafController {
4     @RequestMapping("/hellohtml")
5     public String helloHtml(Map<String, Object> map) {
6         map.put("hello", "from TemplateController.helloHtml");
7         return "/hellohtml";
8     }
9 }

  预览看效果

  关于thymeleaf更多的语法这里就不展开说了。

posted @ 2018-02-08 19:19  无脑仔的小明  阅读(1047)  评论(0编辑  收藏  举报