玩转springboot:thymeleaf模板引擎入门程序
一、前言
常用的模板引擎有:JSP、Velocity、Freemarker、Thymeleaf
但是,Springboot默認是不支持JSP的,默認使用thymeleaf模板引擎。而且,語法更簡單,功能更強大,所以這里介紹一下springboot使用Thymeleaf的實例以及遇到的問題。
二、配置application.properties文件
#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=/**spring.thymeleaf.prefix:設置模板的位置
spring.thymeleaf.suffix:設置模板后綴
說明一下,這些配置不是必須的,如果配置了會覆蓋默認的。
在開發(fā)時建議將spring.thymeleaf.cache設置為false,否則會有緩存,導致頁面沒法及時看到更新后的效果。
比如你修改了一個文件,已經(jīng)update到tomcat,但刷新頁面還是之前的頁面,就是因為緩存引起的。
三、在pom.xml中添加thymeleaf的依賴
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>四、controller測試
/*** @author 歐陽思海* @date 2018/7/25 9:42*/ @Controller public class HelloController {@RequestMapping(value = "/test")public ModelAndView test(ModelAndView mv) {mv.setViewName("/test");mv.addObject("title","歡迎使用Thymeleaf!");return mv;} }五、編寫thymeleaf模板頁面
注意:
spring-boot項目靜態(tài)文件目錄:/src/java/resources/static
spring-boot項目模板文件目錄:/src/java/resources/templates
所以test.html文件在/src/java/resources/templates下。
六、運行效果
在瀏覽器輸入http://localhost:8080/test
這個只是thymeleaf模板的入門程序,更多的看下篇文章,具體講解thymeleaf模板的語法。
總結
以上是生活随笔為你收集整理的玩转springboot:thymeleaf模板引擎入门程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 玩转springboot:默认静态资源和
- 下一篇: 玩转springboot:实现sprin