- 报错:
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
浏览器输入请求可以正常访问了。