您好,在我的项目中,当我尝试验证表单时,即使验证失败也不会显示任何错误消息(即使未提交表单也进入了验证失败块)

这是我的代码

      /****************** 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中显示它,有人可以帮助我吗?

最佳答案

我认为您看不到任何东西,因为在下面的第二行中,您破坏了模型(包括验证错误)并创建了一个新模型。

    if(validateFormData(property, result)) {
     model.addAttribute("property", new Property());  // <------
     return "property/postProperty";


尝试显示作为参数出现的属性,可能您将能够看到验证错误。

    if(validateFormData(property, result)) {
     model.addAttribute("property", property);
     return "property/postProperty";

09-10 07:32
查看更多