英文:
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:
<!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;
}
}
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("/adminDashboard")
public String showAdminDashboard(Model model) {
model.addAttribute("newProd", new ProductDto());
return "AdminDashboard";
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论