发生了模板解析错误(模板:“类路径资源[templates/index.html]”)

huangapple go评论109阅读模式
英文:

An error happened during template parsing (template: "class path resource [templates/index.html]")

问题

以下是你提供的内容的中文翻译:

我尝试打开我的HTML页面,但每当我尝试访问localhost时,我就卡在这些错误上。


白标错误页面

此应用程序没有针对/error的显式映射,因此您看到了它作为回退。

2020年8月11日,中欧夏令时18:24:14
发生了意外错误(类型=内部服务器错误,状态=500)。
模板解析期间发生错误(模板:“class path resource [templates/index.html]”)
org.thymeleaf.exceptions.TemplateInputException: 模板解析期间发生错误(模板:“class path resource [templates/index.html]”)
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
	...

我将附上涉及的文件:

首页控制器:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home(Model model) {
	    model.addAttribute("formData", new User());
	    return "index";
    }

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public String processFormData(User user) {
	    return "results";
    }
}

用户领域类:

public class User {
    private String fullName;
    private int age;
    private boolean employed;
    private String gender;


    public User() {}

    public User(String full_name, int age, boolean employed, String gender) {
	    super();
	    this.fullName = full_name;
	    this.age = age;
	    this.employed = employed;
	    this.gender = gender;
    }

    // GETTER AND SETTERS HERE
}

index.html:

<!doctype html>
<html lang="en" xmlns:th="https://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8" />
    <title>主页</title>
</head>
<body>
    <form action="/create" method="POST" th:object="${formData}">
	    <p>全名:<input type="text" th:field="${formData.fullName}" /></p>
	    <p>年龄:<input type="text" th:field="${formData.age}" /></p>
	    <p>就业:<input type="checkbox" th:field="${formData.employed}" th:value="true"/></p>
	    <p>
		    性别:<br>
		    男性 <input type="radio" th:field="${formData.gender}" th:value="Male" />
		    女性 <input type="radio" th:field="${formData.gender}" th:value="Female" />
	    </p>
	    <p>
		    <input type="submit" value="提交"/>
		    <input type="reset" value="重置"/>
	    </p>
    </form>
</body>
</html>
英文:

I'm trying to open my html page but I'm stuck with these error whenever I try to get access to localhost.


Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Aug 11 18:24:14 CEST 2020
There was an unexpected error (type=Internal Server Error, status=500).
An error happened during template parsing (template: &quot;class path resource [templates/index.html]&quot;)
org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: &quot;class path resource [templates/index.html]&quot;)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100)
at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098)
at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072)
at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362)
at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189)
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1373)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1118)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1057)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:373)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1589)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: org.attoparser.ParseException: Error during execution of processor &#39;org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor&#39; (template: &quot;index&quot; - line 9, col 36)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 48 more
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor &#39;org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor&#39; (template: &quot;index&quot; - line 9, col 36)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:117)
at org.thymeleaf.processor.element.AbstractElementTagProcessor.process(AbstractElementTagProcessor.java:95)
at org.thymeleaf.util.ProcessorConfigurationUtils$ElementTagProcessorWrapper.process(ProcessorConfigurationUtils.java:633)
at org.thymeleaf.engine.ProcessorTemplateHandler.handleStandaloneElement(ProcessorTemplateHandler.java:918)
at org.thymeleaf.engine.TemplateHandlerAdapterMarkupHandler.handleStandaloneElementEnd(TemplateHandlerAdapterMarkupHandler.java:260)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler$InlineMarkupAdapterPreProcessorHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:256)
at org.thymeleaf.standard.inline.OutputExpressionInlinePreProcessorHandler.handleStandaloneElementEnd(OutputExpressionInlinePreProcessorHandler.java:169)
at org.thymeleaf.templateparser.markup.InlinedOutputExpressionMarkupHandler.handleStandaloneElementEnd(InlinedOutputExpressionMarkupHandler.java:104)
at org.attoparser.HtmlElement.handleStandaloneElementEnd(HtmlElement.java:79)
at org.attoparser.HtmlMarkupHandler.handleStandaloneElementEnd(HtmlMarkupHandler.java:241)
at org.attoparser.MarkupEventProcessorHandler.handleStandaloneElementEnd(MarkupEventProcessorHandler.java:327)
at org.attoparser.ParsingElementMarkupUtil.parseStandaloneElement(ParsingElementMarkupUtil.java:96)
at org.attoparser.MarkupParser.parseBuffer(MarkupParser.java:706)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:301)
... 50 more
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property &#39;fullName&#39; of bean class [com.example.demo.domain.User]: Bean property &#39;fullName&#39; is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:622)
at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyValue(AbstractNestablePropertyAccessor.java:612)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:104)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:228)
at org.springframework.web.servlet.support.BindStatus.&lt;init&gt;(BindStatus.java:129)
at org.springframework.web.servlet.support.RequestContext.getBindStatus(RequestContext.java:903)
at org.thymeleaf.spring5.context.webmvc.SpringWebMvcThymeleafRequestContext.getBindStatus(SpringWebMvcThymeleafRequestContext.java:227)
at org.thymeleaf.spring5.util.FieldUtils.getBindStatusFromParsedExpression(FieldUtils.java:306)
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:258)
at org.thymeleaf.spring5.util.FieldUtils.getBindStatus(FieldUtils.java:227)
at org.thymeleaf.spring5.processor.AbstractSpringFieldTagProcessor.doProcess(AbstractSpringFieldTagProcessor.java:174)
at org.thymeleaf.processor.element.AbstractAttributeTagProcessor.doProcess(AbstractAttributeTagProcessor.java:74)
... 63 more

I will attach the files involved:

Home controller:

@Controller
public class HomeController {
@RequestMapping(&quot;/&quot;)
public String home(Model model) {
model.addAttribute(&quot;formData&quot;, new User());
return &quot;index&quot;;
}
@RequestMapping(value = &quot;/create&quot;, method = RequestMethod.POST)
public String processFormData(User user) {
return &quot;results&quot;;
}
}

User domain class:

public class User {
    private String fullName;
    private int age;
    private boolean employed;
    private String gender;


    public User() {}

    public User(String full_name, int age, boolean employed, String gender) {
	    super();
	    this.fullName = full_name;
	    this.age = age;
	    this.employed = employed;
	    this.gender = gender;
    }

    // GETTER AND SETTERS HERE
}

index.html:

&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot; xmlns:th=&quot;https://www.thymeleaf.org/&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot; /&gt;
&lt;title&gt;Home page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form action=&quot;/create&quot; method=&quot;POST&quot; th:object=&quot;${formData}&quot;&gt;
&lt;p&gt;Full name: &lt;input type=&quot;text&quot; th:field=&quot;${formData.fullName}&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Age: &lt;input type=&quot;text&quot; th:field=&quot;${formData.age}&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Employed: &lt;input type=&quot;checkbox&quot; th:field=&quot;${formData.employed}&quot; th:value=&quot;true&quot;/&gt;&lt;/p&gt;
&lt;p&gt;
Gender: &lt;br&gt;
Male &lt;input type=&quot;radio&quot; th:field=&quot;${formData.gender}&quot; th:value=&quot;Male&quot; /&gt;
Female &lt;input type=&quot;radio&quot; th:field=&quot;${formData.gender}&quot; th:value=&quot;Female&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
&lt;input type=&quot;reset&quot; value=&quot;Reset&quot;/&gt;
&lt;/p&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 1

bean class [com.example.demo.domain.User] 的属性 'fullName' 无效:Bean 属性 'fullName' 不可读或具有无效的 getter 方法

Thymeleaf 只能通过 getter 方法访问对象的属性。如果要在 Thymeleaf 中使用对象绑定,还需要确保具有setter和默认构造函数。

通常,无论何时使用Thymeleaf,建议为传递给Thymeleaf的所有对象定义所有的getter、setter和默认构造函数。

英文:

Invalid property 'fullName' of bean class [com.example.demo.domain.User]: Bean property 'fullName' is not readable or has an invalid getter method

Thymeleaf can only access the properties of objects via getters. Also if you were to use object binding with Thymeleaf you would have to make sure you have setters and default constructor.

In general whenever you work with Thymeleaf it is a good idea to define all the getters, setters and default constructor for all objects being passed to thymeleaf.

答案2

得分: -2

你应该将用户的属性可见性更改为公开。
完成了。

来自萨尔瓦多的问候。

英文:

you should change the visibility of the user's properties to public.
and it's done.

greetings from El salvador

huangapple
  • 本文由 发表于 2020年8月12日 00:31:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63362504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定