Thymeleaf未识别对象。

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

Thymeleaf not recognizing object

问题

我尝试按照我拥有的一个用于提交新产品的工作登录页面示例的示例,但是当我尝试启动管理员仪表板时,它返回状态码500,并在启动控制台中显示错误消息:“org.thymeleaf.exceptions.TemplateInputException: 模板解析期间发生错误(模板:“class path resource [templates/AdminDashboard.html]”)...
原因是:java.lang.IllegalStateException: 请求属性中不可用名为'newProd'的BindingResult或普通目标对象"

这是我的代码:

HTML文件:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <link rel="stylesheet"
  7. href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
  8. integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
  9. crossorigin="anonymous">
  10. </head>
  11. <body>
  12. <h1>Admin dashboard</h1>
  13. <form th:action="@{/adminDashboard}" method="post" th:object="${newProd}">
  14. <div class="form-group">
  15. <label class="control-label" for="name"> Product name </label>
  16. <input id="name" class="form-control" th:field="*{name}" required autofocus="autofocus" />
  17. </div>
  18. <div class="form-group">
  19. <label class="control-label" for="storedAmount"> stored Amount </label>
  20. <input id="storedAmount" class="form-control" th:field="*{storedAmount}" required autofocus="autofocus" />
  21. </div>
  22. <div class="form-group">
  23. <label class="control-label" for="price"> Price </label>
  24. <input id="price" class="form-control" th:field="*{price}" required autofocus="autofocus" />
  25. </div>
  26. <div class="form-group">
  27. <div class="row">
  28. <div class="col-sm-6 col-sm-offset-3">
  29. <input type="submit" name="Product-submit" id="Product-submit"
  30. class="form-control btn btn-primary" value="Submit product" />
  31. </div>
  32. </div>
  33. </div>
  34. </form>
  35. </body>
  36. </html>

Controller:

  1. public class MainController {
  2. @GetMapping("/adminDashboard")
  3. public String showAdminDashboard(){
  4. return "AdminDashboard";
  5. }
  6. @PostMapping("/adminDashboard")
  7. public String loginUserAccount(@ModelAttribute("newProd") ProductDto productDto) {
  8. System.out.println(productDto.getName());
  9. return "redirect:/dashboard";
  10. }
  11. }

ProductDto:

  1. package com.reiniskr.registrationloginspring.web.dto;
  2. public class ProductDto {
  3. private String name;
  4. private int storedAmount;
  5. private double price;
  6. public ProductDto(){}
  7. public ProductDto(String name, int storedAmount, double price) {
  8. this.name = name;
  9. this.storedAmount = storedAmount;
  10. this.price = price;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getStoredAmount() {
  19. return storedAmount;
  20. }
  21. public void setStoredAmount(int storedAmount) {
  22. this.storedAmount = storedAmount;
  23. }
  24. public double getPrice() {
  25. return price;
  26. }
  27. public void setPrice(double price) {
  28. this.price = price;
  29. }
  30. }

如果我从HTML文件中删除POST表单,它可以工作,但是如果我使用Thymeleaf进行任何其他操作,它根本不会加载模板。

英文:

I tried following the example that I had of a working login page for submitting a new product but when I try to launch the admin dashboard it returns status code 500 and in the launch console an error stating: "org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/AdminDashboard.html]")
...
Caused by: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'newProd' available as request attribute"

Heres my code:

HTML file:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;en&quot;&gt;
  3. &lt;head&gt;
  4. &lt;meta charset=&quot;UTF-8&quot;&gt;
  5. &lt;title&gt;Title&lt;/title&gt;
  6. &lt;link rel=&quot;stylesheet&quot;
  7. href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&quot;
  8. integrity=&quot;sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u&quot;
  9. crossorigin=&quot;anonymous&quot;&gt;
  10. &lt;/head&gt;
  11. &lt;body&gt;
  12. &lt;h1&gt;Admin dashboard&lt;/h1&gt;
  13. &lt;form th:action=&quot;@{/adminDashboard}&quot; method=&quot;post&quot; th:object=&quot;${newProd}&quot;&gt;
  14. &lt;div class=&quot;form-group&quot;&gt;
  15. &lt;label class=&quot;control-label&quot; for=&quot;name&quot;&gt; Product name &lt;/label&gt;
  16. &lt;input id=&quot;name&quot; class=&quot;form-control&quot; th:field=&quot;*{name}&quot; required autofocus=&quot;autofocus&quot; /&gt;
  17. &lt;/div&gt;
  18. &lt;div class=&quot;form-group&quot;&gt;
  19. &lt;label class=&quot;control-label&quot; for=&quot;storedAmount&quot;&gt; stored Amount &lt;/label&gt;
  20. &lt;input id=&quot;storedAmount&quot; class=&quot;form-control&quot; th:field=&quot;*{storedAmount}&quot; required autofocus=&quot;autofocus&quot; /&gt;
  21. &lt;/div&gt;
  22. &lt;div class=&quot;form-group&quot;&gt;
  23. &lt;label class=&quot;control-label&quot; for=&quot;price&quot;&gt; Price &lt;/label&gt;
  24. &lt;input id=&quot;price&quot; class=&quot;form-control&quot; th:field=&quot;*{price}&quot; required autofocus=&quot;autofocus&quot; /&gt;
  25. &lt;/div&gt;
  26. &lt;div class=&quot;form-group&quot;&gt;
  27. &lt;div class=&quot;row&quot;&gt;
  28. &lt;div class=&quot;col-sm-6 col-sm-offset-3&quot;&gt;
  29. &lt;input type=&quot;submit&quot; name=&quot;Product-submit&quot; id=&quot;Product-submit&quot;
  30. class=&quot;form-control btn btn-primary&quot; value=&quot;Submit product&quot; /&gt;
  31. &lt;/div&gt;
  32. &lt;/div&gt;
  33. &lt;/div&gt;
  34. &lt;/form&gt;
  35. &lt;/body&gt;
  36. &lt;/html&gt;

Controller:

  1. public class MainController {
  2. @GetMapping(&quot;/adminDashboard&quot;)
  3. public String showAdminDashboard(){
  4. return &quot;AdminDashboard&quot;;
  5. }
  6. @PostMapping(&quot;/adminDashboard&quot;)
  7. public String loginUserAccount(@ModelAttribute(&quot;newProd&quot;) ProductDto productDto) {
  8. System.out.println(productDto.getName());
  9. return &quot;redirect:/dashboard&quot;;
  10. }
  11. }

ProductDto:

  1. package com.reiniskr.registrationloginspring.web.dto;
  2. public class ProductDto {
  3. private String name;
  4. private int storedAmount;
  5. private double price;
  6. public ProductDto(){}
  7. public ProductDto(String name, int storedAmount, double price) {
  8. this.name = name;
  9. this.storedAmount = storedAmount;
  10. this.price = price;
  11. }
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getStoredAmount() {
  19. return storedAmount;
  20. }
  21. public void setStoredAmount(int storedAmount) {
  22. this.storedAmount = storedAmount;
  23. }
  24. public double getPrice() {
  25. return price;
  26. }
  27. public void setPrice(double price) {
  28. this.price = price;
  29. }
  30. }

If i remove the POST form in the HTML file it works, but anything other that I do with thymeleaf it just doesnt load the template at all.

答案1

得分: 0

你需要在显示页面之前将一个 ProductDto 的实例添加到 Model 中的 newProd 属性中。

  1. @GetMapping("/adminDashboard")
  2. public String showAdminDashboard(Model model) {
  3. model.addAttribute("newProd", new ProductDto());
  4. return "AdminDashboard";
  5. }
英文:

You need to add an instance of ProductDto to the Model as the newProd attribute before showing the page.

  1. @GetMapping(&quot;/adminDashboard&quot;)
  2. public String showAdminDashboard(Model model) {
  3. model.addAttribute(&quot;newProd&quot;, new ProductDto());
  4. return &quot;AdminDashboard&quot;;
  5. }

huangapple
  • 本文由 发表于 2023年6月26日 04:52:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552358.html
匿名

发表评论

匿名网友

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

确定