我在商业模式中上课:
public class User {
@Size(min = 3, max = 20, message = "Username must be between 3 and 20 characters long")
@Pattern(regexp = "^[a-zA-Z0-9]+$", message = "Username must be alphanumeric with no spaces")
@Column(name = "name")
private String name;
@Id
@GeneratedValue
@Column(name = "user_id")
private Long userID;
@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "userDic", joinColumns = { @JoinColumn(name = "user_id", referencedColumnName = "user_id") }, inverseJoinColumns = {
@JoinColumn(name = "acc_id", referencedColumnName = "acc_id") })
@LazyCollection(value = LazyCollectionOption.FALSE)
private Set<Account> allAccounts; //............. //constructors , setters , getters here
在新用户的jsp输入中通过spring形式发生:
<form:form method="post" action="AddUser" modelAttribute="user">
<table>
<tr>
<td><form:label path="name">First Name</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="*" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Add User" /></td>
</tr>
</table>
</form:form>
这是处理这个jsp的控制器:
@Controller
@SessionAttributes
public class AdminServlet
{
/* private UserValidator validator;
@InitBinder("user")
protected void initBinder(WebDataBinder binder)
{
binder.setValidator(validator);
}*/
@RequestMapping(value = "/")
public ModelAndView showUsers() throws ClassNotFoundException, SQLException
{
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("ATMUsers", MainService.getAllUsers());
mav.addObject("user", new User());
return mav;
}
@RequestMapping(value = "/AddUser", method = RequestMethod.POST)
public String AddUser(@ModelAttribute("user") @Valid User user, BindingResult result)
{
if (!result.hasErrors())
MainService.AddUser(user);
return "redirect:/";
}
}
对字段name
进行正确的验证。但控制器只是重新加载页面,我没有看到任何类型的错误消息。
如果我取消注释控制器中关于UserValidator
和InitBinder
的代码,我会收到错误,但它不是<form:errors path="*" />
中的文本,而是java异常
javax.validation.ConstraintViolationException:对于组[javax.validation.groups.Default,]
的持续时间,类[com.antonbanking.business.User]的验证失败
问题是我做错了只是得到验证错误消息? BTW full code is available in github。
答案 0 :(得分:4)
我自己解决问题 - 问题是AdminServlet.AddUser的回复。因为我做了"redirect:/"
这导致创建包括BindingResult在内的全新对象所以所有关于错误的信息都被删除了。
改变后
@RequestMapping(value = "/AddUser", method = RequestMethod.POST)
public String AddUser(@ModelAttribute("user") @Valid User user, BindingResult result)
{
if (!result.hasErrors())
MainService.AddUser(user);
return "redirect:/";
}
到
@RequestMapping(value = "/AddUser", method = RequestMethod.POST)
public ModelAndView AddUser(@ModelAttribute("user") @Valid User user, BindingResult result)
{
ModelAndView mav = new ModelAndView();
mav.setViewName("index");
mav.addObject("ATMUsers", MainService.getAllUsers());
if (result.hasErrors())
mav.addObject("user", user);
else
{
mav.addObject("user", new User());
MainService.AddUser(user);
}
return mav;
}
一切正常开始。
答案 1 :(得分:0)
在MainService.AddUser(user);
周围捕获异常,并在catch子句中向BindingResult对象添加错误或重定向。
参见BindingResult (addError or reject* from Errors) 另请参阅Validation (without annotation)