【发布时间】:2019-03-17 00:51:30
【问题描述】:
我目前在使用 springboot 时遇到问题,并且出现错误“模板解析期间发生错误(模板:“类路径资源 [templates/mainpage.html])”。
我尝试重新安装不同版本的 lombok,因为我认为这可能是问题所在,但到目前为止似乎没有任何效果。我正在将 Eclipse 的 gradle 用作 IDE。 感谢您提供任何帮助,由于 springBootVersions 不同,发现了一些具有相同问题的线程,但尝试了新旧线程,它也没有为我修复它。
我的 build.gradle 看起来像这样:
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'test'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
compileOnly 'org.projectlombok:lombok:1.18.2'
}
我的控制器:
package test;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ExampleController {
@GetMapping(path = "/")
public String mainpage(Model model) {
return "mainpage";
}
@PostMapping(path = "/")
public String calculate(Model model, Calc cal) {
model.addAttribute("cal", cal);
return "mainpage";
}
}
带有 lombok 的 Calc.java 文件:
@Data
public class Calc {
private Long val1;
private Long val2;
public Long getSum() {
return this.val1 + this.val2;
}
}
还有我的mainpage.html:
<html>
<title>Homepage</title>
<body>
<form method="post">
<input type="text" name="val1" th:value="${cal.val1}"> </input>
<input type="text" name="val2" th:value="${cal.val2}"> </input>
<input type="submit"></input>
</form>
<p th:text="| ${cal.val1} and ${cal.val2} equals ${cal.sum}|"></p>
</body>
</html>
【问题讨论】:
-
"2018-10-13 13:19:12.590 错误 9592 --- [nio-8000-exec-1] oaccC[.[.[/].[dispatcherServlet] : Servlet.service( ) 用于路径 [] 上下文中的 servlet [dispatcherServlet] 引发异常 [请求处理失败;嵌套异常是 org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“类路径资源 [templates/mainpage.html] ")] 根本原因 org.springframework.expression.spel.SpelEvaluationException: EL1007E: 在 null 上找不到属性或字段 'val1'"
-
@buræquete 我认为这是您正在寻找的消息,由于 val1 似乎没有被初始化可能是因为 Calc 没有被正确识别,这可能会再次成为 lombok 的问题,但我将它直接安装到源代码中并通过 eclipse 安装了几次
标签: spring-boot thymeleaf