【问题标题】:Thymeleaf; "an error happened during template parsing"百里香叶; “模板解析期间发生错误”
【发布时间】: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;
    }
}

还有我的ma​​inpage.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


【解决方案1】:

将springBootVersion 降级为“1.5.10.RELEASE”并将以下类添加到您的应用程序包中。

@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Bean
    @Description("Thymeleaf template resolver serving HTML 5")
    public ClassLoaderTemplateResolver templateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("templates/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setCharacterEncoding("UTF-8");
        return templateResolver;
    }

    @Bean
    public ServletContextTemplateResolver servletContextTemplateResolverResolver() {
        final ServletContextTemplateResolver resolver =
                new ServletContextTemplateResolver();
        resolver.setPrefix("/WEB-INF/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode("HTML5");
        resolver.setCacheable(false);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    @Description("Thymeleaf template engine with Spring integration")
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.addTemplateResolver(new UrlTemplateResolver());
        templateEngine.addDialect(new SpringSecurityDialect());
        templateEngine.setTemplateResolver(templateResolver());
        return templateEngine;
    }


    @Bean
    @Description("Thymeleaf view resolver")
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        viewResolver.setCharacterEncoding("UTF-8");
        return viewResolver;
    }
}

【讨论】:

  • 哇——降级到 1.5.10.RELEASE 会破坏我的整个应用程序。还有其他选择吗?
【解决方案2】:

问题来自您的第一个端点,您没有定义 Calc 类型属性,因此 mainpage.html 的解析由于缺少此属性而失败,并且无法将其类类型(字段)映射到html中提到的那些。

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

<input type="text" name="val1" th:value="${cal?.val1}"> </input> 

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

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

    model.addAttribute("cal", new Calc());
    return "mainpage";
}

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

【讨论】:

    猜你喜欢
    • 2015-11-29
    • 2016-08-14
    • 2013-02-03
    • 2015-06-01
    • 2016-07-02
    • 1970-01-01
    • 2020-05-17
    • 1970-01-01
    相关资源
    最近更新 更多