SpringBoot使用thymeleaf模板引擎


(1)、添加pom依赖

  

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

 

  *  SpringBoot1.x 默认的thymeleaf版本低,如果要自定义版本,需要在pom properties 覆写SpringBoot默认的thymeleaf版本号

  

1     <properties>
2         <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
3         <!--thymeleaf3需要布局功能layout2以上版本-->
4         <thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version>
5     </properties>

  

  *  SpringBoot2.x 无需此操作

 

(2)、将Html页面放至classpath:/templates/下,thymeleaf模板引擎将自动渲染

 

  *thymeleaf视图修改后,如何实时生效

  在配置文件中禁用thymeleaf缓存

spring.thymeleaf.cache=false

页面修改完成后Ctrl + F9重新编译(Idea)

浏览器刷新页面


*在控制器重定向跳转至静态资源目录而非thymeleaf目录,在配置类中注册一下转发映射

registry.addViewController("index/html").setViewName("index");

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM