Use commons-validator to validate forms in spring annotation mode

Original: http://www.programgo.com/article/12411299733/Reference
:
spring(3) mvc annotation verification springmodules common validator (annotation verification) http://blog.csdn.net/zheng963/article/details/48681057
combat springmodules common validator http://blog.csdn.net/sunxing007/article/details/4660262

1. Use spring-modules-validation.jar,
2. And introduce commons-validator.jar and related packages,
3. A standard validator-rules.xml file, and custom validation.xml file,
4. Add in *-servlet.xml:
    <!-- Form Validation Management-->
    <bean id="beanValidator"
        class="org.springmodules.validation.commons.DefaultBeanValidator">
        <property name="validatorFactory" ref="validatorFactory" />
    </bean>
    <bean id="validatorFactory"
        class="org.springmodules.validation.commons.DefaultValidatorFactory">
        <property name="validationConfigLocations">
            <list>
                <value>WEB-INF/validator-rules.xml</value>
                <value>WEB-INF/validation.xml</value>
            </list>
        </property>
    </bean>

5. Add to the processing parameters of the validation form:
@ModelAttribute("user") User user, BindingResult bindingResult

6. Before processing the submission add:    
validator.validate(jituanmk, bindingResult);


Example of form validation:
first configure the contents of 1-4 above, and then in the form processing method, do the following:
    @RequestMapping("/userUpdate")
    public String processJituanmkUpdate(@ModelAttribute("user") User user, BindingResult bindingResult, //Add the verification object here, note that these two parameters must be next to each other to facilitate the spring framework binding
        Model model ) throws IOException {
        validator.validate(user, bindingResult);//Use validator.xml to validate the form object
        if (bindingResult.hasErrors()) { //An error occurs, output the error message back to the form page
            logger.error(bindingResult.getFieldErrors());
            model.addAttribute("errors", bindingResult.getFieldErrors());
            return "userModify";
        }
        ... // no error, continue processing


The writing of validaton.xml is consistent with that in struts. It should be noted that
the name value in <form name="user"> is the class name of the validation object with the first letter lowercase. If this is written incorrectly, of course, the correct verification result will not be obtained.
spring-2.5+springmodules-0.9 , the test passed.

Other verification schemes,
1. You can use the @InitBinder annotation to bind data, as detailed in Reference 2, petclinic in the samples in the spring release package.
2. It can be processed by implementing the Validator interface provided by spring.
There may be other solutions, but I feel that for ordinary applications, using validator.xml is the most convenient, after all, at least there is no need to write so much code for validation.


Reference:
1. http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/
2. http://www.infoq.com/cn/articles/spring-2.5-ii- spring-mvc

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326963237&siteId=291194637