springboot学习:spring-boot中如何使用thymeleaf模板引擎

整体步骤:
(1)            在pom.xml中引入thymeleaf;
(2)            如何关闭thymeleaf缓存
(3)            编写模板文件.html

Spring Boot默认就是使用thymeleaf模板引擎的,所以只需要在pom.xml加入依赖即可:

[html]  view plain  copy
  1. <dependency>  
  2.          <groupId>org.springframework.boot</groupId>  
  3.   
  4.          <artifactId>spring-boot-starter-thymeleaf</artifactId>  
  5. </dependency>  

Thymeleaf缓存在开发过程中,肯定是不行的,那么就要在开发的时候把缓存关闭,只需要在application.properties进行配置即可:

[html]  view plain  copy
  1. ########################################################  
  2. ###THYMELEAF (ThymeleafAutoConfiguration)  
  3. ########################################################  
  4. #spring.thymeleaf.prefix=classpath:/templates/  
  5. #spring.thymeleaf.suffix=.html  
  6. #spring.thymeleaf.mode=HTML5  
  7. #spring.thymeleaf.encoding=UTF-8  
  8. # ;charset=<encoding> is added  
  9. #spring.thymeleaf.content-type=text/html  
  10. # set to false for hot refresh  
  11.   
  12. spring.thymeleaf.cache=false  

编写模板文件src/main/resouces/templates/helloHtml.html

[html]  view plain  copy
  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>  
编写访问路径(com.kfit.test.web.TemplateController):

[html]  view plain  copy
  1. package com.kfit.test.web;  
  2.   
  3. import java.util.Map;  
  4. import org.springframework.stereotype.Controller;  
  5. import org.springframework.web.bind.annotation.RequestMapping;  
  6.   
  7.    
  8.   
  9. /**  
  10.   
  11.  * 模板测试.  
  12.   
  13.  * @author Administrator  
  14.   
  15.  *  
  16.   
  17.  */  
  18.   
  19. @Controller  
  20.   
  21. publicclass TemplateController {  
  22.     /**  
  23.   
  24.      * 返回html模板.  
  25.   
  26.      */  
  27.   
  28.     @RequestMapping("/helloHtml")  
  29.     public String helloHtml(Map<String,Object> map){  
  30.   
  31.        map.put("hello","from TemplateController.helloHtml");  
  32.        return"/helloHtml";  
  33.     }  
  34. }  

启动应用,输入地址:http://127.0.0.1:8080/helloHtml 会输出:

Hello.v.2

from TemplateController.helloHtml

使用freemarker
使用freemarker也很简单,
在pom.xml加入freemarker的依赖:

[html]  view plain  copy
  1. <dependency>  
  2.         <groupId>org.springframework.boot</groupId>  
  3.         <artifactId>spring-boot-starter-freemarker</artifactId>  
  4. </dependency>  
剩下的编码部分都是一样的,说下application.properties文件:

[html]  view plain  copy
  1. ########################################################  
  2. ###FREEMARKER (FreeMarkerAutoConfiguration)  
  3. ########################################################  
  4. spring.freemarker.allow-request-override=false  
  5. spring.freemarker.cache=true  
  6. spring.freemarker.check-template-location=true  
  7. spring.freemarker.charset=UTF-8  
  8. spring.freemarker.content-type=text/html  
  9. spring.freemarker.expose-request-attributes=false  
  10. spring.freemarker.expose-session-attributes=false  
  11. spring.freemarker.expose-spring-macro-helpers=false  
  12. #spring.freemarker.prefix=  
  13. #spring.freemarker.request-context-attribute=  
  14. #spring.freemarker.settings.*=  
  15. #spring.freemarker.suffix=.ftl  
  16. #spring.freemarker.template-loader-path=classpath:/templates/#comma-separatedlist  
  17. #spring.freemarker.view-names= #whitelistofviewnamesthatcanberesolved  
com.kfit.test.web.TemplateController:
[html]  view plain  copy
  1. /**  
  2.   * 返回html模板.  
  3.   */  
  4.   
  5.     @RequestMapping("/helloFtl")  
  6.     public String helloFtl(Map<String,Object> map){  
  7.        map.put("hello","from TemplateController.helloFtl");  
  8.        return"/helloFtl";  
  9.     }  

访问地址:http://127.0.0.1:8080/helloFtl

Hello.v.2

猜你喜欢

转载自blog.csdn.net/wjq6940/article/details/78955129