Thymeleaf未识别对象。

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

Thymeleaf not recognizing object

问题

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

这是我的代码:

HTML文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet"
          href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
          crossorigin="anonymous">
</head>
<body>
  <h1>Admin dashboard</h1>


  <form th:action="@{/adminDashboard}" method="post" th:object="${newProd}">
      <div class="form-group">
          <label class="control-label" for="name"> Product name </label>
          <input id="name" class="form-control" th:field="*{name}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <label class="control-label" for="storedAmount"> stored Amount </label>
          <input id="storedAmount" class="form-control" th:field="*{storedAmount}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <label class="control-label" for="price"> Price </label>
          <input id="price" class="form-control" th:field="*{price}" required autofocus="autofocus" />
      </div>
      <div class="form-group">
          <div class="row">
              <div class="col-sm-6 col-sm-offset-3">
                  <input type="submit" name="Product-submit" id="Product-submit"
                         class="form-control btn btn-primary" value="Submit product" />
              </div>
          </div>
      </div>
  </form>

</body>
</html>

Controller:

public class MainController {
    @GetMapping("/adminDashboard")
    public String showAdminDashboard(){
        return "AdminDashboard";
    }

    @PostMapping("/adminDashboard")
    public String loginUserAccount(@ModelAttribute("newProd") ProductDto productDto) {
        System.out.println(productDto.getName());
        return "redirect:/dashboard";
    }
}

ProductDto:

package com.reiniskr.registrationloginspring.web.dto;

public class ProductDto {
    private String name;
    private int storedAmount;
    private double price;

    public ProductDto(){}

    public ProductDto(String name, int storedAmount, double price) {
        this.name = name;
        this.storedAmount = storedAmount;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getStoredAmount() {
        return storedAmount;
    }

    public void setStoredAmount(int storedAmount) {
        this.storedAmount = storedAmount;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}

如果我从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:

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Title&lt;/title&gt;
&lt;link rel=&quot;stylesheet&quot;
href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&quot;
integrity=&quot;sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u&quot;
crossorigin=&quot;anonymous&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Admin dashboard&lt;/h1&gt;
&lt;form th:action=&quot;@{/adminDashboard}&quot; method=&quot;post&quot; th:object=&quot;${newProd}&quot;&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;control-label&quot; for=&quot;name&quot;&gt; Product name &lt;/label&gt;
&lt;input id=&quot;name&quot; class=&quot;form-control&quot; th:field=&quot;*{name}&quot; required autofocus=&quot;autofocus&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;control-label&quot; for=&quot;storedAmount&quot;&gt; stored Amount &lt;/label&gt;
&lt;input id=&quot;storedAmount&quot; class=&quot;form-control&quot; th:field=&quot;*{storedAmount}&quot; required autofocus=&quot;autofocus&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;label class=&quot;control-label&quot; for=&quot;price&quot;&gt; Price &lt;/label&gt;
&lt;input id=&quot;price&quot; class=&quot;form-control&quot; th:field=&quot;*{price}&quot; required autofocus=&quot;autofocus&quot; /&gt;
&lt;/div&gt;
&lt;div class=&quot;form-group&quot;&gt;
&lt;div class=&quot;row&quot;&gt;
&lt;div class=&quot;col-sm-6 col-sm-offset-3&quot;&gt;
&lt;input type=&quot;submit&quot; name=&quot;Product-submit&quot; id=&quot;Product-submit&quot;
class=&quot;form-control btn btn-primary&quot; value=&quot;Submit product&quot; /&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Controller:

public class MainController {
@GetMapping(&quot;/adminDashboard&quot;)
public String showAdminDashboard(){
return &quot;AdminDashboard&quot;;
}
@PostMapping(&quot;/adminDashboard&quot;)
public String loginUserAccount(@ModelAttribute(&quot;newProd&quot;) ProductDto productDto) {
System.out.println(productDto.getName());
return &quot;redirect:/dashboard&quot;;
}
}

ProductDto:

package com.reiniskr.registrationloginspring.web.dto;
public class ProductDto {
private String name;
private int storedAmount;
private double price;
public ProductDto(){}
public ProductDto(String name, int storedAmount, double price) {
this.name = name;
this.storedAmount = storedAmount;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStoredAmount() {
return storedAmount;
}
public void setStoredAmount(int storedAmount) {
this.storedAmount = storedAmount;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}

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 属性中。

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

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

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

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:

确定