`

spring中的commands validator的使用方法

阅读更多
spring 的validate验证需要两个bean(ValidatorFactory、BeanValidator)及两xml文件(validator- rules.xml、validation.xml)的支持,ValidatorFactory用于制造BeanValidator,后者则是在程序中执行校验的控制bean。Validatorrules.xml中定义了各种验证的规则,如字段不为空,字段输入值必须为整数等等。 Validation.xml中则定义了那些bean及bean中的哪些属性字段需要验证,使用哪些验证规则。validate 分为两级验证,客户端验证(javascript)和服务器端验证。以下是使用validate的步骤:

1.       在配置文件中声明ValidatorFactory 和 BeanValidator:

<bean  id="validatorFactory" class="org.springmodules.commons.validator.DefaultValidatorFactory"> -----①

             <property name="validationConfigLocations"> -----②

                <list>

                      <value>/WEB-INF/validator-rules.xml</value>

                      <value>/WEB-INF/validation.xml</value>

                 </list>

             </property>

        </bean>

        <bean id="beanValidator"

class="org.springmodules.commons.validator.DefaultBeanValidator"> -------③

             <property name="validatorFactory">

                     <ref bean="validatorFactory" />  --------④

             </property>

 </bean>

①     声明validatoFactory 这里我们使用spring 的DefaultValidatorFactory

②     定义其validationConfigLocations 属性,将validator-rules.xml和validation.xml传入

③     声明beanValidator 这里我们使用spring的DefaultBeanValidator

④     在其属性中指明要使用的validatorFactory 。这里我们使用刚刚定义的validatorFactory

 

2.       在需要进行验证的controller(即要使用form表单或者command的controller)中声明validate。

 

<bean id="xxxxController" ……>

<property name="commandName" value="userCommand"/>  -----①

<property name="commandClass" value="com.resoft.User"/>   -----②

<property name="validator" ref="beanValidator"/>   -------③

     ……

</bean>

 

①     commandName 用于指明需要验证的 command的名字,这个名字必须和 validation.xml中<form name=" xxxxx "> 所写的名字保持一致。

②     commandClass用于指定这个command的类型。其必须与你jsp提交的form最后形成的command类型相一致。

③     声明该controller使用validator,这里将我们刚刚定义的beanValidator传入进去。

 

3.       在validation.xml文件中,定义你要校验的formbean(或者说是command) ,定义这个bean中有哪几个field需要验证,使用何种规则验证。(注意:这里定义的form name 必须和前面controller中定义的commandName保持一致)以下是几种常用的验证示例:

 

<form-validation>

<formset>

<form name=" userCommand "> -----①

               <field property="userId" depends="required"> -----②     

                   <arg0 key="用户代码" /> -------③                                          

               </field>    

                                                                

①    这里是定义要验证的bean的名字,要求去上面定义的 commandName 相同

②    这里定义要验证的bean属性为 userId ,使用规则为 required(不为空)

③    这里定义的是显示信息,arg0 表示位置 0 的显示信息,显示key 所标明的信息 (如果你还有多个信息就用 arg1,arg2 等)出错信息将会显示为:  用户代码 不能为空 

 

 

<field property="age" depends="required,integer,mask"> -----①

                   <arg0 key="年龄" />

                   <msg name="mask" key="errors.negative"/> -----②

                   <var>

                      <var-name>mask</var-name>

                      <var-value>^[1-9]</var-value> -------③

                   </var>

        </field>

 

①   mask 标记验证,使用正则表达式来约束表单输入值。(如:只能输入数字,字母或指定的字符)

②   msg 标签用来定义 验证规则和出错信息的关联,这里表示当出现 mask 校验失败时,显示 errors.negative指明的信息。

③      整个<var>标签就是用来定义具体的规则的,如<var-name>mask</var-name>指明是用于 mask的规则,<var-value>^[1-9]</var-value> 表示只允许1-9这几个数字开头。

所以上面验证信息是年龄字段不可以为空,必须为整数,而且必须为1-9开头 也就杜绝了负数和零的情况。

          

               <field property="birthday" depends="required,date"> -----①

                  <arg0 key="生日" />

                  <var>

                       <var-name>datePatternStrict</var-name> -----②

                       <var-value>yyyy-MM-dd</var-value>  -----③

                   </var>

        </field>

        </formset>

 

①   使用“date”加入对日期的格式的校验

②         开始定义日期格式,var-name 必须写为 datePatternStrict

③         定义你希望的日期格式如:yyyy-mm-dd  yy/mm/dd 等等都可以。

<field property="startDate" depends="required,date">

              <arg0 key="开始日期" />

              <var>

                   <var-name>datePatternStrict</var-name>

                   <var-value>yyyy-MM-dd</var-value>

                </var>

       </field>

       <field property="endDate" depends="required,date,compareTwoField"> -----①

              <arg0 key="结束日期" />

              <arg1 key="开始日期" />  -----②

              <var>

                   <var-name>datePatternStrict</var-name>

                   <var-value>yyyy-MM-dd</var-value>

            </var>

            <var>

               <var-name>secondField</var-name>  -------③

               <var-value>startDate</var-value>

            </var>

       </field>

 

①         这里演示的是两个日期的验证,要达到开始日期不能晚于结束日期,加入compareTwoField 验证规则

②         定义第2个显示参数agr1 “开始日期”

③         加入一个var 其var-name 为secondField(这个是在程序中写死的,必须写为secondField)

④         var-value 定义为开始日期的属性名,如在本例中为 startDate

注:compareTwoField 是我们自己编写的一个校验规则,用于比较两个字段值大小。

 

4.  在jsp页面中定义错误信息显示语句:

 

<spring:bind path="tableCrashDO.*">   -----①

    <c:if test="${not empty status.errorMessages}">

    <div class="error">  

        <c:forEach var="error" items="${status.errorMessages}">

            <c:out value="${error}" escapeXml="false"/><br />

        </c:forEach>

    </div>

    </c:if>

</spring:bind>

 

①      这里的path 必须要与传入该 jsp 的bean的名字一样。如传入该jsp的数据bean 叫tableCrashDO,path就应该写为tableCrashDO.*。

 

5. 使客户端产生javascript代码:

 

<v:javascript formName="tableCrashDO"    -----①

staticJavascript="false" xhtml="true" cdata="false"/>

<script type="text/javascript" src="<c:url value="scripts/validator.jsp"/>"></script>

 

①      v:javascript 标签是spring的标签,定义在spring-commons-validator.tld 。其中formName必须与validation.xml中form的name保持一致。

如此以来,基本上可以算完成了validate的验证配置。

(特别注意: controller 中的commandName; validation.xml中的form name; jsp中v:javascript标签的formName三者必须保持一致。)

分享到:
评论

相关推荐

    spring-commands-cucumber

    春天命令Cucumber 这个 gem 为实现了cucumber命令。 用法 添加到您的 Gemfile: gem "spring-commands-cucumber

    spring 最新框架jar

    spring-framework-4.3.6.RELEASE - 解包大小为 44.9 MB ## Spring Framework The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications ...

    Laravel开发-commands

    Laravel开发-commands 这是为帮助开发工作中的每个人而创建的Laravel Artisan命令集合。

    Linux Commands

    Linux Commands

    Mastering.Spring.Cloud

    Mastering Spring Cloud Packt Upsell Why subscribe? PacktPub.com Contributors About the author About the reviewer Packt is searching for authors like you Preface Who this book is for What this book ...

    ZPL II Commands for RFID

    ZPL II Commands for RFID

    FreeBSD Commands

    FreeBSD Commands It is useful!

    Unix shell commands

    Unix shell commands,Unix shell commands

    SCSI Block Commands – 3 (SBC-3).pdf

    此标准规定了SCSI Block Commands - 3(SBC-3)命令集的功能要求.SBC-3允许SCSI块逻辑单元(如刚性磁盘)连接到计算机并提供其使用的定义。 该标准与SCSI Block Commands(SBC-2)命令集INCITS 405-2005保持高度兼容...

    Oracle DBA commands

    Oracle DBA commands

    mastering-spring-cloud2018

    focus on describing most commonly used Docker commands, which are used for running and monitoring microservices in containerized environment. You will also learn how to build and run containers using ...

    TPM-Rev-2.0-Part-3-Commands-01.38.pdf

    Trusted Platform Module Library Part 3: Commands This TPM 2.0 Part 3 of the Trusted Platform Module Library specification contains the definitions of the TPM commands. These commands make use of the ...

    Commands with JSP.jsp

    使用想服务器上传jsp执行windows或者linux命令,来获取服务器的资源情况。 Commands with JSP.sjp

    db2 commands

    db2 commands to use when operate db2 database.zip

    Kingkong AT Commands Spec V1.5

    Kingkong AT Commands Spec V1.5

    latex2e common commands

    commom commands of latex

    SCSI Block Commands – 3

    SCSI Block Commands – 3

    ANSYS Commands Reference.pdf

    ansys commands reference,学习ansys必备,可以熟练运用

    SCSI Primary Commands - 4 (SPC-4)

    SCSI Primary Commands - 4 (SPC-4) spc4r25 This is an internal working document of T10, a Technical Committee of Accredited Standards Committee INCITS (InterNational Committee for Information ...

    windows commands2019.pdf

    Windows Commands ,Command-Line Syntax Key ,Commands by Server Role ,Print Command Reference ,Services for Network File System Command Reference ,Remote Desktop Services (Terminal Services) Command ...

Global site tag (gtag.js) - Google Analytics