如何在表单中显示每个字段的错误消息?
How I can show error message for each field in form?
在我的验证器中,我为每个字段添加了一些描述,并希望在 form(.jsp) 中显示错误消息,但是当我为每个字段显示消息时,错误消息是相同的,这是什么问题,我的代码:
验证者
public class StoreValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Store.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.empty", "Name field is empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.empty", "Address field is empty");
}
}
控制器
@Autowired
private StoreValidator storeValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(storeValidator);
}
//post method create store
@RequestMapping(value = "/regStoreSuccessful", method = RequestMethod.POST)
public ModelAndView addStorePost(@Valid @ModelAttribute("storeForm") Store storeForm, BindingResult bindingResult, Principal principal, RedirectAttributes redirectAttrs) throws SQLException {
ModelAndView modelAndView = new ModelAndView("redirect:body");
storeValidator.validate(storeForm,bindingResult);
if(bindingResult.hasErrors()) {
redirectAttrs.addFlashAttribute("error", bindingResult.getFieldError().getDefaultMessage());
//modelAndView.addObject("storeForm", bindingResult.getFieldError().getDefaultMessage());
//modelAndView.addObject("storeForm", storeForm);
//redirectAttrs.addFlashAttribute("errors", bindingResult.getFieldError().getRejectedValue());
//redirectAttrs.addFlashAttribute("storeForm", storeForm);
//..
}
//..
}
表格
<form:form method="POST" action="/regStoreSuccessful" commandName="storeForm">
<table>
<tr>
<td><form:label path="name">Name store</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="name" /></td>
</tr>
<tr>
<td><form:label path="address">Address store</form:label></td>
<td><form:input path="address" /></td>
<td><form:errors path="address" /></td>
</tr>
</form:form>
我也试过使用 <form:errors path = "name" />
,但没有用...
您应该在 hasErrors 方法的模型属性中添加整个对象
并且对于每个输入分别使用 form:errors 每个输入。
<form:errors path = "name" />
<form:errors path = "address" />
if(bindingResult.hasErrors()) {
model.addAttribute("storeForm",storeForm);
//..
}
在我的验证器中,我为每个字段添加了一些描述,并希望在 form(.jsp) 中显示错误消息,但是当我为每个字段显示消息时,错误消息是相同的,这是什么问题,我的代码:
验证者
public class StoreValidator implements Validator {
@Override
public boolean supports(Class<?> clazz) {
return Store.class.equals(clazz);
}
@Override
public void validate(Object target, Errors errors) {
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "name.empty", "Name field is empty");
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address", "address.empty", "Address field is empty");
}
}
控制器
@Autowired
private StoreValidator storeValidator;
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(storeValidator);
}
//post method create store
@RequestMapping(value = "/regStoreSuccessful", method = RequestMethod.POST)
public ModelAndView addStorePost(@Valid @ModelAttribute("storeForm") Store storeForm, BindingResult bindingResult, Principal principal, RedirectAttributes redirectAttrs) throws SQLException {
ModelAndView modelAndView = new ModelAndView("redirect:body");
storeValidator.validate(storeForm,bindingResult);
if(bindingResult.hasErrors()) {
redirectAttrs.addFlashAttribute("error", bindingResult.getFieldError().getDefaultMessage());
//modelAndView.addObject("storeForm", bindingResult.getFieldError().getDefaultMessage());
//modelAndView.addObject("storeForm", storeForm);
//redirectAttrs.addFlashAttribute("errors", bindingResult.getFieldError().getRejectedValue());
//redirectAttrs.addFlashAttribute("storeForm", storeForm);
//..
}
//..
}
表格
<form:form method="POST" action="/regStoreSuccessful" commandName="storeForm">
<table>
<tr>
<td><form:label path="name">Name store</form:label></td>
<td><form:input path="name" /></td>
<td><form:errors path="name" /></td>
</tr>
<tr>
<td><form:label path="address">Address store</form:label></td>
<td><form:input path="address" /></td>
<td><form:errors path="address" /></td>
</tr>
</form:form>
我也试过使用 <form:errors path = "name" />
,但没有用...
您应该在 hasErrors 方法的模型属性中添加整个对象 并且对于每个输入分别使用 form:errors 每个输入。
<form:errors path = "name" />
<form:errors path = "address" />
if(bindingResult.hasErrors()) {
model.addAttribute("storeForm",storeForm);
//..
}