SpringBoot匯入thymeleaf模板,執行報錯。
阿新 • • 發佈:2018-12-12
- 報錯:
SpringBoot匯入thymeleaf模板,執行報錯org.xml.sax.SAXParseException: 元素型別 “link” 必須由匹配的結束標記 終止。
1、新建SpringBoot MAVEN專案後 JAR型別的專案
2、新增pom.xml檔案
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.xdl</groupId> <artifactId>ovls_exam_web</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.7.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <!-- bean掃描、自動配置、@bean定義 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!-- mvc --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- jstl --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!-- jsp api --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 熱部署 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- thymeleaf --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> </project>
3、新增application.properties
#server
server.port=7778
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/exam/
spring.thymeleaf.suffix=.html #可以去掉 預設就是html
4、主啟動類
package cn.xdl.ovls.exam; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ExamWebBootApplication { public static void main(String[] args) { SpringApplication.run(ExamWebBootApplication.class, args); } }
5、編寫controller
package cn.xdl.ovls.exam.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller public class ExamController { @RequestMapping("/exam/home") public ModelAndView home(){ System.out.println("abc"); ModelAndView mav = new ModelAndView(); mav.setViewName("home"); return mav; } }
專案路徑如下圖
6.瀏覽器輸入URL請求:
http://localhost:7778/exam/home
瀏覽器報錯:500錯誤
本地伺服器:後臺錯誤,但是能進Controller,資訊如下
報錯org.xml.sax.SAXParseException: 元素型別 “link” 必須由匹配的結束標記 “” 終止。
類似的錯誤還有:
org.xml.sax.SAXParseException: 元素型別 “meta” 必須由匹配的結束標記 “” 終止。
7.錯誤原因:
8.解決辦法:
1、pom.xml檔案引入HTML5非強制語法校驗
2、追加application.properties定義
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xdl</groupId>
<artifactId>ovls_exam_web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.7.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- bean掃描、自動配置、@bean定義 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- jsp api -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 啟用thymeleaf非嚴格html5檢查 -->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
</dependencies>
</project>
追加application.properties定義
#server
server.port=7778
#thymeleaf
spring.thymeleaf.prefix=classpath:/templates/exam/
#spring.thymeleaf.suffix=.html
#啟用thymeleaf非嚴格檢查
#spring.thymeleaf.content-type=text/html
#spring.thymeleaf.cache=false
spring.thymeleaf.mode =LEGACYHTML5
瀏覽器輸入請求可以正常訪問了。