出處:http://www.cnblogs.com/Laupaul/archive/2012/03/15/2398360.htmlhtml
http://www.blogjava.net/focusJ/archive/2010/11/15/367272.htmljava
使用基於XML配置方式實現輸入校驗時,Action也須要繼承ActionSupport,而且提供校驗文件,校驗文件和action類放在同一個包下,文件的取名格式爲:ActionClassName-validation.xml。ActionClassName爲action的簡單類名,-validation爲固定寫法。若是Action類爲cn.validate.action.UserAction,那麼該文件的取名爲:UserAction-validation.xml。正則表達式
下面是xml配置信息express
1 <!DOCTYPE validators PUBLIC 2 "-//Apache Struts//XWork Validator 1.0.2//EN" 3 "http://struts.apache.org/dtds/xwork-validator-1.0.2.dtd"> 4 5 <validators> 6 <field name="username"> 7 <field-validator type="requiredstring"><!-- 必填字符串校驗器 --> 8 <param name="doTrim">true</param><!-- 去掉兩邊空格 --> 9 <message>用戶名不能爲空</message> 10 </field-validator> 11 </field> 12 <field name="mobile"> 13 <field-validator type="requiredstring"><!-- 必填字符串校驗器 --> 14 <param name="doTrim">true</param><!-- 去掉兩邊空格 --> 15 <message>手機號碼不能爲空</message> 16 </field-validator> 17 <field-validator type="regex"><!-- 正則表達式校驗器 --> 18 <param name="expression"><![CDATA[^1[358]\d{9}$]]></param><!-- 表達式 --> 19 <param name="caseSensitive">false</param><!-- 區分大小寫 --> 20 <param name="trim">true</param><!-- 去掉兩邊空格 --> 21 <message>手機號碼格式不正確</message> 22 </field-validator> 23 </field> 24 </validators>
action類:apache
1 package cn.validate.action; 2 3 import com.opensymphony.xwork2.ActionContext; 4 import com.opensymphony.xwork2.ActionSupport; 5 6 @SuppressWarnings("serial") 7 public class XmlValidationAction extends ActionSupport { 8 private String username; 9 private String mobile; 10 11 public String getUsername() { 12 return username; 13 } 14 15 public void setUsername(String username) { 16 this.username = username; 17 } 18 19 public String getMobile() { 20 return mobile; 21 } 22 23 public void setMobile(String mobile) { 24 this.mobile = mobile; 25 } 26 27 public String update() { 28 ActionContext.getContext().put("message", "update success!"); 29 return "success"; 30 } 31 32 public String sace() { 33 ActionContext.getContext().put("message", "save success!"); 34 return "success"; 35 } 36 }
對於指定方法進行校驗咱們又該怎麼作呢?數組
很簡單,咱們只須要改下xml文件名便可。格式爲ActionClassName-ActionName-validation.xml,其中ActionName爲struts.xml中action名稱。框架
好比:在XmlValidationAction中的update()進行校驗,咱們把xml文件名改成 XmlValidationAction-validation_update-validation.xml便可,其餘的都同樣。jsp
另外struts還提供了不少校驗器,我發一些經常使用的出來,至於用法,你們能夠看框架的源碼,所屬包爲com.opensymphony.xwork2.validator.validators測試
struts框架提供的校驗器
required必填校驗器
requiredstring必填字符串校驗器
stringlength字符串長度校驗器
regex正則表達式校驗器
int整數校驗器
double雙精度浮點型校驗器
filedexpression字段OGNL表達式校驗器
email郵件校驗器
url網址校驗器
date日期校驗器
conversion轉換校驗器
visitor用於校驗action中的符合屬性
expressionOGNL表達式校驗器ui
/********************************************************更新**********************************************************/
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <%@ taglib uri="/struts-tags" prefix="s"%> 3 <% 4 String path = request.getContextPath(); 5 String basePath = request.getScheme() + "://" 6 + request.getServerName() + ":" + request.getServerPort() 7 + path + "/"; 8 %> 9 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 10 <html> 11 <head> 12 <base href="<%=basePath%>"> 13 <title>My JSP 'fail.jsp' starting page</title> 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 </head> 20 <body> 21 <s:form action="user/login.action" validate="true"> 22 <table 23 style="border-style: solid; border-color: lightblue; position: absolute; top: 30px"> 24 <tbody> 25 <tr> 26 <td> 27 <div 28 style="margin-center: 32px; border-style: solid; border-color: lightblue; border-width: 2px;"> 29 <a>用戶名:</a> 30 <input id="username" name="user.name" type="text" /> 31 </div> 32 </td> 33 </tr> 34 <tr> 35 <td> 36 <div 37 style="margin-center: 32px; border-style: solid; border-color: lightblue; border-width: 2px"> 38 <a>密 碼:</a> 39 <input id="password" name="user.password" type="password" /> 40 </div> 41 </td> 42 </tr> 43 <!-- 彈出出錯信息 --> 44 <tr> 45 <td> 46 <div id="msg"><s:fielderror/></div> 47 </td> 48 </tr> 49 <tr> 50 <td> 51 <div> 52 <span><input id="submit" type="submit" value="登陸" /> 53 </span> 54 </div> 55 </td> 56 </tr> 57 </tbody> 58 </table> 59 </s:form> 60 </body> 61 </html>
這裏有個地方須要注意:form中要加入validate=「true」這個屬性。
Action.action這個挺簡單的就是常規的action寫法,在execute中驗證表單,或本身封裝方法驗證表單,可是不能繼承validate()方法,由於action執行的時候實現檢察validate方法的,若是這樣的話就配置重複了,不過有什麼結果我沒有測試(沒啥意義)。
1 <constant name="struts.custom.i18n.resources" value="globalMessages"></constant> 2 <include file="defaule.xml" /> 3 <!-- 有關用戶的操做 --> 4 <package name="user" extends="struts-default" namespace="/user"> 5 <action name="login" class="bbs.action.UserAction"> 6 <result name="success">/system/list.action</result> 7 <result name="input">/login.jsp</result> 8 </action> 9 </package>
有一點有必要提一下,action中必須配置input這個result,由於若是驗證失敗後struts2會自動轉向到input的result,無論你的action中配置的驗證失敗的result是什麼。因此這個input屬性的result不能落下。