Thymeleaf在Spring Boot中无法解析字段。

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

thymeleaf cannot resolve field in springboot

问题

您有一个简单的表单,将一个名为obj的对象和一个名为description的值发送到您的控制器,控制器将其提交到您的数据库。您正在使用一个JPA存储库。起初,您遇到了在表单中解析th:object="${obj}"时的问题,但在阅读了堆栈溢出帖子后,您为对象添加了一个变量定义,问题解决了,但是仍然无法在输入标签中使用th:field,如果只是写它,您会得到500个内部错误。th:name=可以正常工作,但我需要在我的项目中使用th:field

HTML页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--<thymesVar id="obj" type="springframework.controllers.view"/>-->
<form  th:action="@{/p/post}" th:object="${obj}" method="post">
    <input type="text" th:name="description" />
<!--    <td><input type="text" th:field="*{description}"/></td> error happen here -->
    <input type="submit" value="submit">
</form>
</body>
</html>

控制器

@Controller
@RequestMapping("/p")
public class View {
    @PostMapping("post")
    public String post(@ModelAttribute("obj") UnitOfMeasure unitOfMeasure){
        unitOfMeasureRepository.save(unitOfMeasure);
        return "home";
    }
}

错误日志

ERROR 14380 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template "post": An error happened during template parsing (template: "class path resource [templates/post.html]")

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/post.html]")

希望这能帮助您解决问题。

英文:

I have a simple form that send an object named obj and a value named description to my controller and controller post it to my data base. I am using a jpa repository. At first I had a problem for resolving th:object=&quot;${obj}&quot; in my form but after reading a stack overflow posts I added a var definition for my object and it fixed the problem but I still can't use th:field in my input tag and if I just write it, I get 500 internal error. th:name= works without any problem but I need to use th:field in my project.

html page

&lt;!DOCTYPE html&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:th=&quot;http://www.thymeleaf.org&quot;&gt;
&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;title&gt;Title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--/*@thymesVar id=&quot;obj&quot; type=&quot;springframework.controllers.view&quot;*/--&gt;
&lt;form  th:action=&quot;@{/p/post}&quot; th:object=&quot;${obj}&quot; method=&quot;post&quot;&gt;
    &lt;input type=&quot;text&quot; th:name=&quot;description&quot; /&gt;
&lt;!--&lt;td&gt;&lt;input type=&quot;text&quot; th:field=&quot;*{description}&quot;/&gt;&lt;/td&gt; error happen here --&gt;
    &lt;input type=&quot;submit&quot; value=&quot;submit&quot;&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

controller

@Controller
@RequestMapping(&quot;/p&quot;)
public class view {
    @PostMapping(&quot;post&quot;)
    public String post(@ModelAttribute(&quot;obj&quot;)UnitOfMeasure unitOfMeasure){
        unitOfMeasureRepository.save(unitOfMeasure);
        return &quot;home&quot;;
    }
}

error log

ERROR 14380 --- [nio-8080-exec-1] org.thymeleaf.TemplateEngine             : [THYMELEAF][http-nio-8080-exec-1] Exception processing template &quot;post&quot;: An error happened during template parsing (template: &quot;class path resource [templates/post.html]&quot;)

org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: &quot;class path resource [templates/post.html]&quot;)
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) ~[thymeleaf-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:362) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.thymeleaf.spring5.view.ThymeleafView.render(ThymeleafView.java:189) ~[thymeleaf-spring5-3.0.11.RELEASE.jar:3.0.11.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:998) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:890) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:634) ~[tomcat-embed-core-9.0.12.jar:9.0.12]
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:875) ~[spring-webmvc-5.1.2.RELEASE.jar:5.1.2.RELEASE]
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:741) ~[tomcat-embed-core-9.0.12.jar:9.0.12]

答案1

得分: 0

在向模型请求对象之前,您需要将其提供给模型。Thymeleaf 不会为您执行此操作。更多信息请参考 https://javamondays.com/problems-with-thymeleaf/

英文:

Before asking for an Object from the Model you need to provide it into the model. Thymeleaf won't do that for you. More here https://javamondays.com/problems-with-thymeleaf/

答案2

得分: 0

在这种情况下,thyme-leaf 没有问题,错误发生是因为我没有将对象添加到模型中。

 @GetMapping("/m")
    public String vri(Model model){
        model.addAttribute("obj", new UnitOfMeasure());
        return "post";
    }
    @PostMapping("/post")
    public String post(@ModelAttribute("obj") UnitOfMeasure unitOfMeasure, Model model){
       // model.addAttribute("obj", unitOfMeasure);

        unitOfMeasureRepository.save(unitOfMeasure);
        return "home";
    }
英文:

in this case there is no problem with thyme-leaf, error happen because of i didn't add the object to the model

 @GetMapping(&quot;/m&quot;)
    public String vri(Model model){
        model.addAttribute(&quot;obj&quot;, new UnitOfMeasure());
        return &quot;post&quot;;
    }
    @PostMapping(&quot;/post&quot;)
    public String post(@ModelAttribute(&quot;obj&quot;) UnitOfMeasure unitOfMeasure, Model model){
       // model.addAttribute(&quot;obj&quot;, unitOfMeasure);

        unitOfMeasureRepository.save(unitOfMeasure);
        return &quot;home&quot;;
    }

huangapple
  • 本文由 发表于 2020年8月1日 18:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204153.html
匿名

发表评论

匿名网友

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

确定