thymeleaf和spring boot的集成踩坑总结(练习项目是老卫的博客项目)

bug1:
2019-08-08 16:44:38.102 ERROR 19810 — [nio-8080-exec-8] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-8] Exception processing template “users/list”: Error resolving template “users/list”, template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template “users/list”, template might not exist or might not be accessible by any of the configured Template Resolvers
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_201]
在这里插入图片描述
这个问题是ModelAndView里面定向的问题,不知道怎么解决???
解决方案是自己吧路径写错了因为list.html文件应该是在template/users下.我竟然把它放到了template/fragement下.真想把自己锤死,竟然花了一下午时间,因为一直认为是配置的问题.这个问题解决后发现真正的问题来了,
在这里插入图片描述
**bug2:**就是thymeleaf在gradle.build里面的配置问题,控制台部分报错代码:org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: “class path resource [templates/users/list.html]” - line 4, col 7)…Caused by: org.attoparser.ParseException: (Line = 4, Column = 7) Malformed markup: Attribute “xmlns:th” appears more than once in element…

在这里插入图片描述
解决办法:删除下面图中白色圈出来的地方(因为不能同时出现两个,这样会起冲突,因为是spring boot整合thyeleaf,所以使用 xmlns:th="http://www.thymeleaf.org"这一个引用就可以了.答案引用于https://blog.csdn.net/qq_41426326/article/details/88837112十分感谢啊)
在这里插入图片描述
老卫讲的这个项目是gradle wapper,没有所谓的pom.xml,下面代码是gradle.build文件包含了对thymeleaf的基本的配置等,因为在解决上述bug时查到thymeleaf的版本也比默认支持的2.0.?,但是配置成高版本好一点

buildscript {
       //用于定义动态属性
        ext{
              springBootVersion = '2.1.7.REALEASE'
       }
        //自定义thymeleaf
        ext['thymeleaf.version']='3.0.3.RELEASE'
        ext['thymeleaf-layout-dialect.version']='2.1.1'
	repositories {
		mavenCentral()
	}
	dependencies {
		classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.5.21.RELEASE'     
	}
} 
plugins {
	id 'java'
}
apply plugin: 'org.springframework.boot'
group = 'com.spring.thymeleaf'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
	mavenCentral()
}
dependencies {
         //添加Thymeleaf的依赖
        compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	    compile 'org.springframework.boot:spring-boot-starter-web'
	    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

猜你喜欢

转载自blog.csdn.net/peopleware1/article/details/99441449