我已经构建了我的控制器、客户端和地址类(模型)以及用于索引和添加的模板。当我尝试测试验证时,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:";
}
}
相关分类