胸腺“模板解析期间发生错误”

时间:2018-10-11 20:33:53

标签: spring-boot thymeleaf

我目前在springboot上遇到问题,并且出现错误“模板解析期间发生错误(模板:“类路径资源[templates / mainpage.html]]”)。

我尝试重新安装不同版本的lombok,因为虽然这可能是问题所在,但到目前为止似乎没有任何效果。我正在使用gradle和Eclipse作为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>

2 个答案:

答案 0 :(得分:0)

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 :(得分:0)

问题来自于您的第一个端点,您尚未定义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";
}

"?"运算符(安全导航运算符)用于覆盖空情况,例如cal?.val1表示使用val1属性的cal字段值,如果{{1} }不能为空,否则请使用空。