Struts2输入校验(二) 验证框架
当前位置:以往代写 > JAVA 教程 >Struts2输入校验(二) 验证框架
2019-06-14

Struts2输入校验(二) 验证框架

Struts2输入校验(二) 验证框架

副标题#e#

一 文件名约定:

1) 验证XML的文件名: Action类名-validation.xml ,并与相关的Action类 放在一起.

2) 当一个Action中有多个业务要领时 : Action 类 名-映射名- validation.xml

什么是映射名:<action name="login"…/> ,name属性的 值:login就是映射名,千万留意,许多人写错

例子:

URL 是:http://localhost/struts/user_addUser. action

struts.xml是:

Xml代码  

<action name="user_*" class="ssh.org.web.UserAction" method=" {1}">
<result name="success">/user/success.jsp</result>
<result name="input">/user/user.jsp</result>
</action>
 

匹配 后<action name="user_*"…/>  实为<action name="user_addUser" …/>

所以验证框架XML文件名是: UserAction-user_addUser -validation.xml

二 验 证不通过期,会执行addFieldError ()要领,在JSP页面,要用 <s:fielderror />显示信息 .

关于addFieldError,请看第6讲:6 addActionError与addFieldError

三 例子

Registration-validation.xml  : 用于校验Registration.java类

Xml代码   

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">

<validators>

<!–
非字段校验器
<validator type="requiredstring">
<param name="fieldName">username</param>
<message>username should not be blank!</message>
</validator>

<validator type="stringlength">
<param name="fieldName">username</param>
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</validator>
–>

字段校验器–>
<field name="username">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message>username should not be blank!</message>
</field-validator>

<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>username should be between ${minLength} and ${maxLength}</message>
</field- validator>
</field>


#p#副标题#e#

<field name="password">
<field-validator type="requiredstring">
<message>password should not be blank! </message>
</field-validator>

<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>password should be between ${minLength} and ${maxLength}</message>
</field- validator>
</field>

<field name="repassword">
<field-validator type="requiredstring">
<message>repassword should not be blank! </message>
</field-validator>

<field-validator type="stringlength">
<param name="minLength">6</param>
<param name="maxLength">10</param>
<message>repassword should be between ${minLength} and ${maxLength}</message>
</field- validator>
</field>

<field name="age">
<field- validator type="required">
<message>age should not be blank! </message>
</field-validator>

<field-validator type="int">
<param name="min">1</param>
<param name="max">150</param>
<message>age should be between ${min} and ${max}</message>
</field-validator>
</field>

<field name="birthday">
<field-validator type="required">
<message>birthday should not be blank! </message>
</field-validator>

<field-validator type="date">
<param name="min">2001-01- 01</param>
<param name="max">2003-12- 31</param>
<message>birthday should be between ${min} and ${max} </message>
</field-validator>
</field>

<field name="graduation">
<field-validator type="required">
<message>graduation should not be blank! </message>
</field-validator>

<field-validator type="date">
<param name="min">2005-01- 01</param>
<param name="max">2007-12- 31</param>
<message>graduation should be between ${min} and ${max} </message>
</field-validator>
</field>
</validators>

#p#副标题#e#

四 Struts2内建的校验器有:

Xml代码

#p#分页标题#e#

<validators>

<validator name="required"  class="com.opensymphony.xwork2.validator.validators.RequiredFieldValidator"/>< br />
<validator name="requiredstring"  class="com.opensymphony.xwork2.validator.validators.RequiredStringValidator"/>

<validator name="int"  class="com.opensymphony.xwork2.validator.validators.IntRangeFieldValidator"/>< br />
<validator name="long"  class="com.opensymphony.xwork2.validator.validators.LongRangeFieldValidator"/>

<validator name="short"  class="com.opensymphony.xwork2.validator.validators.ShortRangeFieldValidator"/> ;

<validator name="double"  class="com.opensymphony.xwork2.validator.validators.DoubleRangeFieldValidator"/&g t;

<validator name="date"  class="com.opensymphony.xwork2.validator.validators.DateRangeFieldValidator"/>

<validator name="expression"  class="com.opensymphony.xwork2.validator.validators.ExpressionValidator"/>

<validator name="fieldexpression"  class="com.opensymphony.xwork2.validator.validators.FieldExpressionValidator"/> ;

<validator name="email"  class="com.opensymphony.xwork2.validator.validators.EmailValidator"/>

<validator name="url"  class="com.opensymphony.xwork2.validator.validators.URLValidator"/>

<validator name="visitor"  class="com.opensymphony.xwork2.validator.validators.VisitorFieldValidator"/>
<validator name="conversion"  class="com.opensymphony.xwork2.validator.validators.ConversionErrorFieldValidator" ;/>

<validator name="stringlength"  class="com.opensymphony.xwork2.validator.validators.StringLengthFieldValidator"/& gt;

<validator name="regex"  class="com.opensymphony.xwork2.validator.validators.RegexFieldValidator"/>

<validator name="conditionalvisitor"  class="com.opensymphony.xwork2.validator.validators.ConditionalVisitorFieldValidator&q uot;/>

</validators>

    关键字:

在线提交作业