我正在学习spring,我在表单验证方面遇到了问题。我可以正常地创建对象,但当我提供的数据应该给我的形式,我有一个例外的信息。我正在发送异常问题:
2021-02-07 14:26:00.048 INFO 5272 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 0 ms
There were some errors
PVModule Model name must have at least 3 chars and max 30
PVModule Manufacturer must have at least 3 chars and max 30
2021-02-07 14:26:04.463 ERROR 5272 --- [io-8080-exec-10] org.thymeleaf.TemplateEngine : [THYMELEAF][http-nio-8080-exec-10] Exception processing template "pvModulesForm": Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "pvModulesForm" - line 18, col 32)
org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "pvModulesForm" - line 18, col 32)
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'pvModule' available as request attribute.....
pvmodule类包括getter、setters和构造函数all和no参数。下面可以找到用于创建对象的字段。
private int id;
@NotNull
@Size(min = 3, max = 30, message = "Manufacturer must have at least 3 chars and max 30")
private String manufacturer;
@NotNull
@Size(min = 3, max = 30, message = "Model name must have at least 3 chars and max 30")
private String model;
@NotNull
@Range(min = 0, max = 99999, message = "Price of the PV module must be in range 0 and 99999")
private double price;
@NotNull
private ModuleType type;
@NotNull
@Range(min = 0, max = 99999, message = "Power of the PV module must be in range 0 and 99999")
private int power;
控制器使用方法创建有效的光伏组件:
@GetMapping("/newPVModule")
public String newModulePV(Model model) {
model.addAttribute("pvModule", new PVModule());
return "pvModulesForm";
}
@PostMapping("/PVModules")
public String saveModule(@Valid PVModule pvModule, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
System.out.println("There were some errors");
bindingResult.getAllErrors().forEach(error -> {
System.out.println(error.getObjectName() + " " + error.getDefaultMessage());
}
);
return "pvModulesForm";
} else {
modulesService.saveModule(pvModule);
return "redirect:/PVModules";
}
}
html表单:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Creating new PV Module</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<link rel="stylesheet" th:href="@{/webjars/bootstrap/4.5.3/css/bootstrap.min.css}"/>
<script th:src="@{/webjars/jquery/3.5.1/jquery.min.js}"></script>
<script th:src="@{/webjars/bootstrap/4.5.3/bootstrap.min.js}"></script>
</head>
<body>
<div class="container">
<form th:object="${pvModule}" th:action="@{/PVModules}" th:method="post" class="form-horizontal">
<div class="form-group">
<label class="control-label">Manufacturer</label>
<input type="text" th:field="*{manufacturer}" class="form-control" />
<p th:if="${#fields.hasErrors('manufacturer')}" th:errors="*{manufacturer}"/>
</div>
<div class="form-group">
<label class="control-label">Model</label>
<input type="text" th:field="*{model}" class="form-control" />
<p th:if="${#fields.hasErrors('model')}" th:errors="*{model}"/>
</div>
<div class="form-group">
<label class="control-label">Price</label>
<input type="number" step="0.01" class="form-control" th:field="*{price}"/>
<p th:if="${#fields.hasErrors('price')}" th:errors="*{price}"/>
</div>
<div>
<label>Module Type</label>
<select th:field="*{type}">
<option th:each="type : ${T(pl.PVcalc.CalculatorPV.Domain.ModuleType).values()}"
th:value="${type}"
th:text="${type}">
</option>
<p th:if="${#fields.hasErrors('type')}" th:errors="*{type}"/>
</select>
</div>
<div class="form-group">
<label class="control-label">Module Power</label>
<input type="number" class="form-control" th:field="*{power}"/>
<p th:if="${#fields.hasErrors('power')}" th:errors="*{power}"/>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Add module</button>
</div>
</form>
</div>
</body>
</html>
我试图在前面的文章中找到答案,但老实说,我检查了关于controller中modeldattribute的建议,我不知道现在应该怎么做。
暂无答案!
目前还没有任何答案,快来回答吧!