英文:
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="${obj}" 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
<!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
@Controller
@RequestMapping("/p")
public class view {
    @PostMapping("post")
    public String post(@ModelAttribute("obj")UnitOfMeasure unitOfMeasure){
        unitOfMeasureRepository.save(unitOfMeasure);
        return "home";
    }
}
error log
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]")
	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("/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";
    }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论