验证Spring混淆以呈现错误消息

时间:2013-06-22 15:26:07

标签: spring jsp spring-mvc attributes controller

我尝试渲染错误消息,但不会出现:

我有验证器:

@Component
class ValidatorFBOSSearch implements Validator {

    @SuppressWarnings("rawtypes")
    @Override
    public boolean supports(Class clazz) {

        return FormBackObjectSearch.class.equals(clazz);
    }

    @Override
    public void validate(Object obj, Errors error) {    

        FormBackObjectSearch fbos = (FormBackObjectSearch)obj;  

        if ("".equals(fbos.particularDate)){            
            error.rejectValue("particularDate", "required.listOfDates", "This field is required");  
        }       
    }
}

它必须贬低我在支持对象形式的字段是否具有输入值。 rejectValue有三个参数。首先 - 在我的支持对象的模型属性中查找值,Secong在属性文件中查找错误消息(我的属性文件位于resourses / error forlder)方法中的第三个参数表示它是否无法在错误中找到消息属性文件将它呈现为默认值,这是我在servlet-context.xml中的代码

<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

这是我的messageMourse的xml配置,用于在servlet-context.xml中获取Validator的错误:

<!-- Resolves error messages for validator from /Education/src/main/webapp/resources/errors-->
    <beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <beans:property name="basename" value="errors/errormessages"/>
    </beans:bean>

这里处理请求的控制器代码的和平:

@RequestMapping(value="/search", method=RequestMethod.GET)
        public void search(Model model) {           
            FormBackObjectSearch fbos = new FormBackObjectSearch();
            model.addAttribute("fbosAttribute", fbos);      
        }


        @RequestMapping(value ="/result", method=RequestMethod.POST)
        public String extract(@RequestParam String nameOfInstitution,
                              @RequestParam String particularDate,
                              @RequestParam String typeOfInstitution,
                              @ModelAttribute("fbosAttribute") FormBackObjectSearch fbos,
                              BindingResult result, RedirectAttributes redirectAttributes, 
                              Model model) throws Exception {

            ValidatorFBOSSearch validatorFBOS = new ValidatorFBOSSearch();
                validatorFBOS.validate(fbos, result);

                if(result.hasErrors()) {
                    redirectAttributes.addFlashAttribute("fbosAttribute", fbos);    
                    return "redirect:/search";


                } else {

                    if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution =="") {                   
                        controllerSupportClass.findWithDateAndName(nameOfInstitution, particularDate, model);                   
                    } else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution !="") {                    
                        controllerSupportClass.findWithAddedDateAndType(typeOfInstitution, particularDate, model);                  
                    } else if(particularDate !="" && nameOfInstitution =="" && typeOfInstitution ==""){         
                        controllerSupportClass.findWithAddedDate(particularDate, model);    
                    } else if(particularDate !="" && nameOfInstitution !="" && typeOfInstitution !="") {
                        throw new Exception("Search by choose all parameters is not exceptable");   
                    } else {    
                        throw new Exception("You didn't put any search parameters");    
                    }

                }
            return "search";
        }

这是我的jsp的和平:

<form:form action="result" method="post" modelAttribute="fbosAttribute" >

<table>

<tr>
<th>Date for extracting:</th> 
<td><form:select  path="particularDate">
<form:option value=""> -Choose date-</form:option>
<form:options items="${listOfDates}"></form:options>
</form:select> <td><form:errors path="particularDate" cssClass="error"/></td>
</td>

</tr>
</table>
</form:form>

问题是我看不到错误信息出现。我试图使用Flash属性在骑行后出现错误,但没有任何事情发生。因为我在这里发现,当我使用rederect时,它会删除我的模型和错误并启动新模型。但是如何利用闪存属性来解决我的问题呢?谢谢

1 个答案:

答案 0 :(得分:0)

要为特定控制器注册验证器,您需要使用“@InitBimder”注释。

@InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(new ValidatorFBOSSearch());
}

有关如何注册验证器的详细信息,请参阅我的答案: Validation in Spring MVC


我认为你的支票陈述错了:

if ("".equals(fbos.particularDate)){

如果用户什么都不输入,那么String不会为空(""),它将是null。因此使用:

if (fbos.particularDate == null || fbos.particularDate.isEmpty())