我已經構建了我的控制器、客戶端和地址類(模型)以及用于索引和添加的模板。當我嘗試測試驗證時,spring 返回:出現意外錯誤(類型=內部服務器錯誤,狀態=500)。在組 [javax.validation.groups.Default, ] 約束違規列表期間,類 [com.pooltracker.models.Address] 的驗證失?。篬 ConstraintViolationImpl{interpolatedMessage='Invalid', propertyPath=zipCode, rootBeanClass=class com .pooltracker.models.Address, messageTemplate='Invalid'} ConstraintViolationImpl{interpolatedMessage='may not be null', propertyPath=state, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='{javax.validation.constraints.NotNull .message}'} ConstraintViolationImpl{interpolatedMessage='不能為空', propertyPath=street, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='不能為空'我了解到 hibernate 無法處理驗證地址類。我偶然發現了 bean 驗證的主題。我對此很陌生,并試圖找到解決我的問題的最佳方法,而不會在目前的兔子洞中走得太遠。下面是我的控制器和模型。@Controller@RequestMapping("clients")public class ClientController { @Autowired private ClientDao clientDao; @Autowired private AddressDao addressDao; @RequestMapping(value = "") public String index(Model model) { model.addAttribute("clients", clientDao.findAll()); model.addAttribute("title", "My Clients"); return "clients/index"; } @RequestMapping(value = "add", method = RequestMethod.GET) public String displayAddClientForm(Model model) { model.addAttribute("title", "Add Client"); model.addAttribute(new Client()); return "clients/add"; } @RequestMapping(value = "add", method = RequestMethod.POST) public String processAddClientForm(@ModelAttribute @Valid Client newClient, Errors errors, Model model) { if (errors.hasErrors()) { model.addAttribute("title", "Add Client"); model.addAttribute("client", newClient); return "clients/add"; } clientDao.save(newClient); return "redirect:"; }}
嘗試使用 java spring boot、hibernate 和 thmyeleaf
幕布斯6054654
2021-07-02 14:00:19