嗨,在我的项目中,当我试图验证我的表单时,它不会显示任何错误消息,即使验证失败(即使表单没有提交并进入验证失败块)
以下是我的代码
/****************** 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中?有人可以帮助我吗?
发布于 2014-06-02 05:47:05
我认为你看不到任何东西,因为在下面的第二行,你销毁了你的模型(包括你的验证错误)并创建了一个新的模型。
if(validateFormData(property, result)) {
model.addAttribute("property", new Property()); // <------
return "property/postProperty";尝试显示作为参数传入的属性,您可能会看到验证错误。
if(validateFormData(property, result)) {
model.addAttribute("property", property);
return "property/postProperty";https://stackoverflow.com/questions/23984618
复制相似问题