action配置中一定要設定input返回頁面
新增驗證只要建立驗證的xml檔案
在action同包下,建立:action類名-validation.xml
如:validateaction建立validateaction-validation.xml
注意:1.要驗證的方法不能叫input.
2.這樣配置在form表單中要在中action寫好名稱,
不能寫action="validate_",然後這樣會找不到對應的配置檔案,跳過驗證.
3.如果驗證出錯,返回input頁面時,那些存在valuestack中的值會丟失,可以將action實現preparable介面,
然後prepare()方法裡初始化新增頁面需要的值.
4.如果使用preparable介面,必須在action配置中新增.
這樣prepare()才能得到form提交的引數.
保證欄位的值不是空值null.空字串不是空值null.true
please enter a mail
invalid mail
please enter a user name保證字段不是空值null,也不是空白(empty).
param:trim(boolean) ->true->去除前後空格
true驗證字段值是否可以轉換為乙個整數.please enter a user name
false
please enter a password
param: min(int);max(int)
1999驗證給定日期欄位的值是否在乙個給定的範圍內.2010
year:1999-2010
param:max(date);min(date)
給定的string值是否是乙個電子郵件位址1999-01-01
2010-01-01
birthday:1999-2010
invalid email給定的string值是否是乙個合法的url(要有字首)
invalid url驗證給定字段是否滿足乙個ognl表示式.
區別:expression 不是乙個字段驗證程式,失敗時將生成乙個動作錯誤.(jsp中呼叫才顯示出錯資訊)
fieldexpression 是乙個字段驗證程式,失敗時將丟擲乙個字段錯誤.(對欄位驗證)
param:expression(string)ognl表示式
expression:
public class expressiontestactionfieldexpression:max > min
maximum temperature must be greater than minimum temperature
public class fieldexpressiontestaction把同乙個驗證程式配置檔案用於多個動作(對乙個bean寫驗證檔案,每個使用的action只要引用)max > min
maximum temperature must be greater than minimum temperature
//userbean如果另乙個action對userbean使用另乙個標準的驗證,可以建立新的驗證檔案public class userbean
和userbean放在同乙個包中)
使用者名稱必須
1899
age must be between 18 and 99
//action的validation.xml
使用者:
檢查對某個屬性進行型別轉換是否會導致乙個轉換錯誤3050
age must be between 30 and 50
//另乙個action的validation.xml
specific
使用者1:
an age must be an integer.驗證乙個非空的字段值是不是足夠的長度
param:minlength(int);maxlength(int);trim(boolean)
6給定的值是否與乙個給定的正規表示式匹配14length:6-14
param:expression(string)正規表示式;casesensitive(boolean)是否區別大小寫,預設為true;trim(boolean)是否去除前後空格
利用validateable介面實現驗證,實現void validate()方法.invalid phone number or invalid format
actionsupport類已經實現了這個介面
//繼承actionsupport要建立乙個普通的驗證程式(非欄位驗證程式),擴充套件validatorsupport類.驗證失敗要從validate方法呼叫addactionerror方法.public class user extends actionsupport
//驗證方法
public void validate()
}}
要建立乙個字段驗證程式,擴充套件fieldvalidatorsupport類.驗證失敗要從validate方法呼叫addfielderror方法.
如果要能接受引數,要在類中定義乙個相應的屬性,並生成get,set.
public class strongpasswordvalidator extends fieldvalidatorsupport在src下建立validators.xmlpublic int getminlength()
//驗證方法
public void validate(object object) throws validationexception
if ((minlength > -1) && (value.length() < minlength)) else if (!ispasswordstrong(value))
}private static final string group_1 = "abcdefghijklmnopqrstuvwxyz";
private static final string group_2 = "abcdefghijklmnopqrstuvwxyz";
private static final string group_3 = "0123456789";
protected boolean ispasswordstrong(string password)
string character = password.substring(i, i + 1);
system.out.println("character:" + character);
if (group_1.contains(character))
if (group_2.contains(character))
if (group_3.contains(character))
}return (ok1 && ok2 && ok3);
}
}
<?xml version="1.0" encoding="utf-8"?>
8password must be at least 8 characters long
and contains at least one lower case character,
one upper case character, and a digit.
struts2 驗證框架
驗證框架 validate 第一種方式 繼承actionsupport類重寫validate 方法 表示提交到此action所有請求都會執行驗證。eg public classloginaction extendsactionsupport publicstring execute override...
Struts2 框架驗證
一 對於輸入校驗struts2提供了兩種實現方法 1 採用手工編寫 實現。2 基於xml配置方式實現。注意 配置驗證檔案actionname alias validation.xml 常用 使用基於xml配置方式實現輸入校驗時,action也需要繼承actionsupport 並且提供校驗檔案和ac...
struts2 自動驗證框架
1.配置strus2環境,加入相關的jar。2.頁面 1 引入struts2的標籤庫 taglib prefix s uri struts tags 2 顯示驗證錯誤資訊 表單資料 user.email user.username user.password user.birthday theme ...