嗨,在我的项目中,当我尝试验证我的表单时,即使验证失败,它也不会显示任何错误消息(即使表单未提交并进入验证失败块)
这是我的代码
/****************** Post Method *************/
@RequestMapping(value="/property", method = RequestMethod.POST)
public String saveOrUpdateProperty(@ModelAttribute("property") Property property,
BindingResult result,
Model model,
HttpServletRequest request) throws Exception {
try {
if(validateFormData(property, result)) {
model.addAttribute("property", new Property());
return "property/postProperty";
}
}
/********* Validate Block *************/
private boolean validateFormData(Property property, BindingResult result) throws DaoException {
if (property.getPropertyType() == null || property.getPropertyType().equals("")) {
result.rejectValue("propertyType", "Cannot Be Empty !", "Cannot Be Empty !");
}
if (property.getTitle() == null || property.getTitle().equals("")) {
result.rejectValue("title", "Cannot Be Empty !", "Cannot Be Empty !");
}
return (result.hasFieldErrors() || result.hasErrors());
}
但是当我调试时,我可以看到下面一个
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'property' on field 'title': rejected value [null]; codes [Cannot Be Empty !.property.title,Cannot Be Empty !.title,Cannot Be Empty !.java.lang.String,Cannot Be Empty !]; arguments []; default message [Cannot Be Empty !]
这就是我在 jsp 文件中的显示方式
<div class="control-group">
<div class="controls">
<label class="control-label"><span class="required">* </span>Property Type</label>
<div class="controls">
<form:input path="title" placeholder="Pin Code" cssClass="form-control border-radius-4 textField"/>
<form:errors path="title" style="color:red;"/>
</div>
</div>
</div>
虽然当我在调试时看到下面的事件(1 错误是正确的)
org.springframework.validation.BeanPropertyBindingResult: 1 errors
为什么在jsp中不显示任何人可以帮助我?