我试图在电话号码输入中添加验证,但在访问索引时却坚持解决此错误。
错误讯息
由以下原因引起:org.attoparser.ParseException:执行处理器org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor时出错(模板:“索引”-第76行,第73行)
由以下原因引起:java.lang.IllegalStateException:Bean名称“ phonenumber”的BindingResult和普通目标对象都不能用作请求属性
控制器
@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
相关分类