Announcement Announcement Module
Collapse
No announcement yet.
Spring 3 integration with Commons Validation framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring 3 integration with Commons Validation framework

    Hi ,


    I am newbie to Spring framework. I have built a very small sample application to start with , using Spring 3 MVC module.
    Application consists of single screen which asks for UserName and Password. On submit , I would be displaying values entered on next page. Now, I wanted to put validation in my application. I wanted to try both client and server side validations so I tried integrating commons validator framework with Spring 3 MVC. Following is what I did :

    In my validation-servlet.xml , I wrote :

    Code:
     <bean id="validatorFactory" class="org.springmodules.validation.commons.DefaultValidatorFactory">
    	 <property name="validationConfigLocations">
    	      <list>
    	         <value>/WEB-INF/validation.xml</value>
    	         <value>/WEB-INF/validator-rules.xml</value>
    	      </list>
    	  </property>
     </bean>   
        
     <bean id="beanValidator" class="org.springmodules.validation.commons.DefaultBeanValidator">
       	<property name="validatorFactory" ref="validatorFactory"/>
     </bean>

    (to be continued .. 10000 characters exceeded )

  • #2
    Following is my validator-rules.xml ( which I copied from some post ) :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE form-validation PUBLIC
              "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
              "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
     
    <form-validation>
        <global>
            <validator name="required"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateRequired"
                methodParams="java.lang.Object,
                            org.apache.commons.validator.ValidatorAction,
                            org.apache.commons.validator.Field,
                            org.springframework.validation.Errors"
                msg="errors.required">
            </validator>
            <validator name="requiredif"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateRequiredIf"
                methodParams="java.lang.Object,
                                   org.springframework.validation.ErrorsAction,
                                   org.apache.commons.validator.Field,
                                   org.springframework.validation.Errors"
                msg="errors.required" />
     
            <validator name="validwhen" msg="errors.required"
                classname="org.apache.struts.validator.validwhen.ValidWhen" method="validateValidWhen"
                methodParams="java.lang.Object,
                           org.springframework.validation.ErrorsAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors" />
     
            <validator name="minlength"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateMinLength"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.minlength"
                jsFunction="org.apache.commons.validator.javascript.validateMinLength" />
     
            <validator name="maxlength"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateMaxLength"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.maxlength"
                jsFunction="org.apache.commons.validator.javascript.validateMaxLength" />
     
            <validator name="mask"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateMask"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.invalid" />
     
            <validator name="byte"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateByte"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.byte" jsFunctionName="ByteValidations" />
     
            <validator name="short"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateShort"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.short" jsFunctionName="ShortValidations" />
     
            <validator name="integer"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateInteger"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.integer" jsFunctionName="IntegerValidations" />
     
            <validator name="long"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateLong"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.long" />
     
            <validator name="float"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateFloat"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.float" jsFunctionName="FloatValidations" />
     
            <validator name="double"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateDouble"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.double" />
     
            <validator name="date"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateDate"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.date" jsFunctionName="DateValidations" />
     
            <validator name="intRange"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateIntRange"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="integer" msg="errors.range" />
     
            <validator name="floatRange"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateFloatRange"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="float" msg="errors.range" />
     
            <validator name="doubleRange"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateDoubleRange"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="double" msg="errors.range" />
     
            <validator name="creditCard"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateCreditCard"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.creditcard" />
     
            <validator name="email"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateEmail"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.email" />
     
            <validator name="url"
                classname="org.springmodules.validation.commons.FieldChecks" method="validateUrl"
                methodParams="java.lang.Object,
                           org.apache.commons.validator.ValidatorAction,
                           org.apache.commons.validator.Field,
                           org.springframework.validation.Errors"
                depends="" msg="errors.url" />
        </global>
    </form-validation>

    Following is my validation.xml :

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE form-validation PUBLIC
              "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
              "http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
     
    <form-validation>
           <formset>
                <form name="validationForm">
                    <field property="uname" depends="required">
                    </field>
                </form>
           </formset>
    </form-validation>

    Comment


    • #3
      following is my only Controller ( annotations are used ) :


      Code:
      @Controller("ValidationController")
      public class ValidationController {
      
          @Autowired
          @Qualifier("beanValidator")
          private Validator beanValidator;  
      	
      	@RequestMapping(value="/validation.do",method=RequestMethod.GET)
      	public String pre(ModelMap map){
      		
      		map.put("validationModel", new ValidationModel());
      		return "validation";
      	}
      	
      	
      	@RequestMapping(value="/validationSubmit.do",method=RequestMethod.POST)
      	public String post(@ModelAttribute ValidationModel model, BindingResult result){
      		
      		  beanValidator.validate(model, result);
      		  
      	        if(result.hasErrors()) {           
      	            return "validation";
      	        }
      		
      	        return "validation";
      	}
      }


      and following is form tag in validation.jsp :

      Code:
      <form:form id="validationForm" name="validationForm" commandName="validationForm"
      		modelAttribute="validationModel" action="validationSubmit.do">
      		<table>
      			<tr>
      				<td>User Name</td>
      				<td>
      					<form:input path="uname"></form:input>
      					<form:errors path="uname"/>				                                      </td>
      			</tr>
      			<tr>
      				<td>Password</td>
      				<td>
      					<form:input path="pwd"></form:input>
      				</td>
      			</tr>
      			<tr>
      				<td><input type="submit" value="Validate" />
      				</td>
      			</tr>
      		</table>
      	</form:form>



      When I run this application , page with username and password textboxes appear. If I dont put any value in username and do submit, I am expecting that defined validation (Required) should run . but , "result.hasErrors()" in controller returns "false" ( meaning validation is not working) . Can any one pl help me out in this .

      Any sort of example link/post/article would also be of great help. Thanks

      Comment


      • #4
        In Spring, Validator framework plays the role of validating data and displaying the error message from property file. There is an interface org.springframework.validation ...
        http://www.concretepage.com/spring/v...ng-example.php

        Comment

        Working...
        X