百里香叶;“模板解析期间发生错误”

Noo*_*oob 5 thymeleaf spring-boot

I\xc2\xb4m 当前在使用 springboot 时遇到问题,并且出现错误“模板解析期间发生错误(模板:“类路径资源 [templates/mainpage.html])”。

\n\n

我\xc2\xb4ve尝试重新安装不同版本的lombok,因为我认为这可能是问题所在,但到目前为止似乎没有任何效果。我\xc2\xb4m 使用 gradle 和 Eclipse 作为 IDE。\n感谢任何帮助,由于不同的 springBootVersions,发现了一些具有相同问题的线程,但尝试了旧的和新的,它也没有\xc2\xb4t 为我修复它。

\n\n

我的build.gradle看起来像这样:

\n\n
buildscript {\next {\n    springBootVersion = \'2.0.3.RELEASE\'\n}\nrepositories {\n    mavenCentral()\n}\ndependencies {\n    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")\n}\n}\n\napply plugin: \'java\'\napply plugin: \'eclipse\'\napply plugin: \'org.springframework.boot\'\napply plugin: \'io.spring.dependency-management\'\n\ngroup = \'test\'\nversion = \'0.0.1-SNAPSHOT\'\nsourceCompatibility = 1.8\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n  compile(\'org.springframework.boot:spring-boot-starter-thymeleaf\')\n  compile(\'org.springframework.boot:spring-boot-starter-web\')\n  runtime(\'org.springframework.boot:spring-boot-devtools\')\n  testCompile(\'org.springframework.boot:spring-boot-starter-test\')\n  compileOnly \'org.projectlombok:lombok:1.18.2\'\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的控制器

\n\n
package test;\n\nimport org.springframework.stereotype.Controller;\nimport org.springframework.ui.Model;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.PostMapping;\n\n@Controller\npublic class ExampleController {\n\n    @GetMapping(path = "/")\n    public String mainpage(Model model) {\n        return "mainpage";\n    }\n\n    @PostMapping(path = "/")\n    public String calculate(Model model, Calc cal) {\n        model.addAttribute("cal", cal);\n        return "mainpage";\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

带有 lombok 的Calc.java文件:

\n\n
@Data\npublic class Calc {\n\n    private Long val1;\n    private Long val2;\n\n    public Long getSum() {\n        return this.val1 + this.val2;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

还有我的mainpage.html

\n\n

\r\n
\r\n
buildscript {\next {\n    springBootVersion = \'2.0.3.RELEASE\'\n}\nrepositories {\n    mavenCentral()\n}\ndependencies {\n    classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")\n}\n}\n\napply plugin: \'java\'\napply plugin: \'eclipse\'\napply plugin: \'org.springframework.boot\'\napply plugin: \'io.spring.dependency-management\'\n\ngroup = \'test\'\nversion = \'0.0.1-SNAPSHOT\'\nsourceCompatibility = 1.8\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n  compile(\'org.springframework.boot:spring-boot-starter-thymeleaf\')\n  compile(\'org.springframework.boot:spring-boot-starter-web\')\n  runtime(\'org.springframework.boot:spring-boot-devtools\')\n  testCompile(\'org.springframework.boot:spring-boot-starter-test\')\n  compileOnly \'org.projectlombok:lombok:1.18.2\'\n}\n
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n

bur*_*ete 2

问题源于您的第一个端点,您没有定义Calc类型属性,因此解析mainpage.htmltype 属性,因此由于缺少此属性而

您应该使用?Thymeleaf 中的运算符(这是处理此问题的正确方法);

<input type="text" name="val1" th:value="${cal?.val1}"> </input> 
Run Code Online (Sandbox Code Playgroud)

Calc或者只是在第一个端点中传递一个空对象作为属性;

@GetMapping(path="/")
public String mainpage(Model model) {

    model.addAttribute("cal", new Calc());
    return "mainpage";
}
Run Code Online (Sandbox Code Playgroud)

"?"运算符(安全导航运算符)是为了覆盖 null 情况,例如cal?.val1表示使用属性val1的字段值cal,如果cal非 null,则使用空。