程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Struts2輸入校驗(二) 驗證框架

Struts2輸入校驗(二) 驗證框架

編輯:關於JAVA

一 文件名約定:

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>

<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>

四 Struts2內建的校驗器有:

Xml代碼

<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>

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved