我试图为电话号码输入添加验证,但我在访问索引时坚持解决此错误。
错误消息
由以下原因引起:org.attoparser.ParseException:执行期间出错 处理器 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (模板:"索引" - 第 76 行,第 73 行(
原因:java.lang.IllegalStateException:既不是 BindingResult 也不是 Bean 名称"电话号码"的纯目标对象可作为请求提供 属性
索引.html
<div class="myForm">
<form th:action="@{/save}" method="post" th:object="${phonebook}">
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Update or Create</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<input type="hidden" class="form-control" id="id" name="id" value=""/>
</div>
<div class="form-group">
<label for="surname" class="col-form-label">Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" value=""/>
</div>
<div class="form-group">
<label for="firstname" class="col-form-label">First Name:</label>
<input type="text" class="form-control" id="firstname" name="firstname" value=""/>
</div>
<div class="form-group">
<label for="phonenumber" class="col-form-label">Phone Number:</label>
<input type="text" class="form-control" th:field="*{phonenumber}" id="phonenumber" name="phonenumber" value=""/>
<span th:if="${#fields.hasErrors('phonenumber')}" class="help-block" th:errors="*{phonenumber}"></span>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<input type="submit" class="btn btn-primary" value="Save"/>
</div>
</div>
</div>
</div>
</form>
控制器
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String showPage(Model model, @RequestParam(defaultValue = "0") int page) {
model.addAttribute("data", phonebookRepository.findAll(PageRequest.of(page, 10)));
model.addAttribute("currentPage", page);
return "index";
}
@PostMapping("/save")
public String save(@Valid Phonebook p, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/";
}else {
phonebookRepository.save(p);
}
return "redirect:/";
}
电话簿
@Entity
@Table(name = "Phonebook")
public class Phonebook {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Integer id;
@NotNull
@Size(max=15, message = "Max symbols is 15")
@Column(name = "phonenumber", length = 15, nullable = false)
private String phonenumber;
@Column(name = "surname", length = 50, nullable = false)
private String surname;
@Column(name = "firstname", length = 50, nullable = false)
private String firstname;
//getters and setter
我有 2 种方法来解决这个问题:--
1.( 使用 @RequestParam 获取电话簿对象,如下所示:--
@PostMapping("/save")
public String save(@Valid @RequestParam("phonenumber")String phonenumber, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/";
}else {
phonebookRepository.save(p);
}
return "redirect:/";
}
OR -- 使用 @ModelAttribute 获取窗体的值,如下所示 :--
1.( 创建一个新的电话簿对象并添加模型属性:-
@RequestMapping(value = {"/"}, method = RequestMethod.GET)
public String showPage(Model model, @RequestParam(defaultValue = "0") int page) {
//your code
model.addAttribute("phoneBook", new Phonebook());
return "index";
}
2.( 您的百里香叶/HTML 页面中的更改(提交后使用th:object发送您的电话簿对象(:--
<form th:action="@{/save}" method="post" th:object="${phoneBook}">
// your code
</form>
3.( 然后使用 @ModelAttribute 绑定这样的值:--
@PostMapping("/save")
public String save(@Valid @ModelAttribute("phoneBook")Phonebook p, BindingResult bindingResult) {
if (bindingResult.hasErrors()) {
return "redirect:/";
}else {
phonebookRepository.save(p);
}
return "redirect:/";
}
4.(最后是你的电话簿类与getter和setter方法。