Struts2驗證框架

2021-05-25 04:42:36 字數 4075 閱讀 9148

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提交的引數.

true

please enter a mail

invalid mail

保證欄位的值不是空值null.空字串不是空值null.

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)

1999-01-01

2010-01-01

birthday:1999-2010

給定的string值是否是乙個電子郵件位址

invalid email

給定的string值是否是乙個合法的url(要有字首)

invalid url

驗證給定字段是否滿足乙個ognl表示式.

區別:expression 不是乙個字段驗證程式,失敗時將生成乙個動作錯誤.(jsp中呼叫才顯示出錯資訊)

fieldexpression 是乙個字段驗證程式,失敗時將丟擲乙個字段錯誤.(對欄位驗證)

param:expression(string)ognl表示式

expression:

public class expressiontestaction 

max > min

maximum temperature must be greater than minimum temperature

fieldexpression:

public class fieldexpressiontestaction 

max > min

maximum temperature must be greater than minimum temperature

把同乙個驗證程式配置檔案用於多個動作(對乙個bean寫驗證檔案,每個使用的action只要引用)

//userbean

public class userbean

和userbean放在同乙個包中)

使用者名稱必須

1899

age must be between 18 and 99

//action的validation.xml

使用者:

如果另乙個action對userbean使用另乙個標準的驗證,可以建立新的驗證檔案

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)是否去除前後空格

invalid phone number or invalid format

利用validateable介面實現驗證,實現void validate()方法.

actionsupport類已經實現了這個介面

//繼承actionsupport

public class user extends actionsupport

//驗證方法

public void validate()

}}

要建立乙個普通的驗證程式(非欄位驗證程式),擴充套件validatorsupport類.驗證失敗要從validate方法呼叫addactionerror方法.

要建立乙個字段驗證程式,擴充套件fieldvalidatorsupport類.驗證失敗要從validate方法呼叫addfielderror方法.

如果要能接受引數,要在類中定義乙個相應的屬性,並生成get,set.

public class strongpasswordvalidator extends fieldvalidatorsupport 

public 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);

}

}

在src下建立validators.xml

<?xml version="1.0" encoding="utf-8"?>

8

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