解決方法一:
原因:在使用springboot的過程中,如果使用thymeleaf作為模板文件,則要求HTML格式必須為嚴格的html5格式,必須有結束標簽,否則會報錯。
在application.yml中添加以下配置
spring: thymeleaf: prefix: classpath:/templates/ mode: HTML cache: false encoding: UTF-8 # 新版本不支持content-type: text/html,故新寫法 servlet: content-type: text/html
再在pom.xml 添加以下依賴
<dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency>
解決方法二:
原因:Spring Boot沒有解析出靜態資源文件位置
<build> <!-- 配置將哪些資源文件(靜態文件/模板文件/mapper文件)加載到tomcat輸出目錄里 --> <resources> <resource> <directory>src/main/java</directory><!--java文件的路徑--> <includes> <include>**/*.*</include> </includes> <!-- <filtering>false</filtering>--> </resource> <resource> <directory>src/main/resources</directory><!--資源文件的路徑--> <includes> <include>**/*.*</include> </includes> <!-- <filtering>false</filtering>--> </resource> </resources> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
解決方法三:
原因:這個是我自己的鍋,我原先使用的是freemaker引擎,文件格式為.ftl,故切換thymeleaf引擎后沒有改為.html,修改文件后綴即可。
解決方法四:
原因:可能是由於找不到前端VIew路徑的原因。
具體參考我的后台(注意ModelAndView的值):
@GetMapping("/listUser") public ModelAndView listUser(){ ModelAndView model = new ModelAndView("user/list"); model.addObject("userList",userservice.listUser()); return model; }
項目結構:
實在找不到,application.yml中加入:
spring:
thymeleaf:
prefix: classpath:/templates/