使用thymeleaf在springboot中訪問HTML頁面

1.在pom.xml中引入thymeleaf包

<!-- 在SpringBoot中訪問HTML的依賴 -->
  <dependency>
       <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
   </dependency>
<!-- 解除thymeleaf對HTML語法的嚴查,不引入的話會有很多語法錯誤 -->
  <dependency>
       <groupId>net.sourceforge.nekohtml</groupId>
       <artifactId>nekohtml</artifactId>
       <version>1.9.21</version>
  </dependency>


2.application.properties配置

spring.mvc.view.prefix=templates/      
spring.mvc.view.suffix=.html

//prefix這個路徑的值一定是templates,所以HTML頁面必須放在templates目錄下,完整路徑爲:src/main/resources/templates
//suffix是指文件後綴,設置這個值以後在路由映射匹配時可以設略前綴


3.完成以上配置以後,就可以輕鬆訪問HTML文件了


@RequestMapping(value ="/index")
    public String index(){
        return "index";
    }


這樣index路由就映射到index.HTML了
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章