简体   繁体   English

使用 Thymeleaf 处理 HTML 模板的模板解析期间发生错误

[英]An error happened during template parsing using Thymeleaf to process a HTML template

I try to process a template with ThymeLeaf but i get org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "generatedTemplate.html") .我尝试使用 ThymeLeaf 处理模板,但我得到org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "generatedTemplate.html")

This is my code:这是我的代码:

ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode(TemplateMode.HTML);

TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
Context context = new Context();

String html = templateEngine.process("generatedTemplate", context);

I tried to change the path by adding a prefix like this but it didn't help:我试图通过添加这样的前缀来更改路径,但它没有帮助:

templateResolver.setPrefix("src/main/resources/");

Can this error be caused by something else than a path problem?此错误可能是由路径问题以外的其他原因引起的吗?

If needed i can give more details about my project structure.如果需要,我可以提供有关我的项目结构的更多详细信息。

Thanks谢谢

EDIT:编辑:

Here's the stack trace, it may help:这是堆栈跟踪,它可能会有所帮助:

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "src/test/resources/generatedTemplate.html")

    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:235)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:649)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1059)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1048)
    at com.oxway.report.services.classes.ReportBuilder.buildReport(ReportBuilder.java:102)
    at com.oxway.report.ReportBuilderTest.sendReportDefinition(ReportBuilderTest.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:252)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.io.FileNotFoundException: ClassLoader resource "src/test/resources/generatedTemplate.html" could not be resolved
    at org.thymeleaf.templateresource.ClassLoaderTemplateResource.reader(ClassLoaderTemplateResource.java:130)
    at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:223)
    ... 35 more

It happened that it was a path problem as expected.碰巧这是一个路径问题,正如预期的那样。 It comes from the fact that the default path change if i call the method from test class or from main class.如果我从测试 class 或主要 class 调用方法,则默认路径会发生变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Spring Boot & Thymeleaf:模板解析时出错 - Java Spring Boot & Thymeleaf: An error happened during template parsing 模板解析时出错 Spring 启动 Thymeleaf - An error happened during template parsing Spring boot Thymeleaf 使用 Spring Boot 解析模板时出错 - An error happened during template parsing using Spring Boot 模板解析时出错(模板:“类路径资源 [templates/index.html]”) - An error happened during template parsing (template: “class path resource [templates/index.html]”) 模板解析时出错(模板:“类路径资源[templates/addWunsch.html]”) - An error happened during template parsing (template: "class path resource [templates/addWunsch.html]") TemplateInputException: 模板解析时发生错误(模板:“类路径资源 [templates/index.html]”) - TemplateInputException: An error happened during template parsing (template: “class path resource [templates/index.html]”) 模板解析时出错(模板:“类路径资源[templates/hello.html] - An error happened during template parsing (template: "class path resource [templates/hello.html] 模板解析时出错(模板:“类路径资源 [templates/index.html]” - An error happened during template parsing (template: "class path resource [templates/index.html]" Thymeleaf 模板解析错误 - Thymeleaf template parsing error 模板解析时出错(模板:“类路径资源[templates/user-form/user-signup.html]”) - An error happened during template parsing (template: "class path resource [templates/user-form/user-signup.html]")
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM