最近在學習springbooot2 和 thymeleafhtml
程序文件java
application.properties文件配置:web
#thymeleaf spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML spring.thymeleaf.check-template-location=true # 靜態文件請求匹配方式 spring.mvc.static-path-pattern=/** # 修改默認的靜態尋址資源目錄 spring.resources.static-locations = classpath:/templates/,classpath:/resources/,classpath:/static/,classpath:/public/ #熱部署生效 spring.devtools.restart.enabled=true
在編譯的時候,發現一直報這個錯誤:spring
Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
以後再網上找了各類答案,發現都不能使用mvc
-------------------------------------------------------------------------------------------------------------------app
下面是正確的方案:maven
在pom.xml中引入以下配置spring-boot
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <resources> <resource> <!--加載資源目錄--> <directory>src/main/resources</directory> <includes> <!--加載配置文件--> <include>**/*.xml</include> <include>**/*.properties</include> <!--加載模板文件--> <include>**/*.html</include> <!--加載靜態文件--> <include>/static/</include> </includes> </resource> </resources> </build>
在pom.xml中引入的文件,這樣application.properties文件中可註釋文件路徑配置。學習
server.port=9099 #thymeleaf #spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.cache=false spring.thymeleaf.servlet.content-type=text/html spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8 spring.thymeleaf.mode=HTML spring.thymeleaf.check-template-location=true # 靜態文件請求匹配方式 #spring.mvc.static-path-pattern=/** # 修改默認的靜態尋址資源目錄 #spring.resources.static-locations = classpath:/templates/,classpath:/resources/,classpath:/static/,classpath:/public/ #熱部署生效 spring.devtools.restart.enabled=true
LoginController中:ui
package com.java.seckill.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/login") public class LoginController { @RequestMapping("/index") public String Index() { return "login"; } @RequestMapping("/login") @ResponseBody public Boolean login(){ return true; } }
直接訪問:http://localhost:9099/login/index