1.配置strus2環境,加入相關的jar。
2.頁面:
1)引入struts2的標籤庫<
%@taglib
prefix = "s" uri="/struts-tags" %>
2)顯示驗證錯誤資訊
表單資料:
user.email
" />
user.username" />
user.password" />
user.birthday" theme="******" /> 【也可以用普通的】
3)action:
public class useraction extends actionsupport
4)struts.xml的配置:
5)驗證的xml檔案
[檔名可以於
action的類名-請求的name的值-validation.xml命名]
如本例中使用的驗證xml檔案名字為:useraction-register-validation.xml
【或者其他命名規則】
驗證的xml檔案跟action在同乙個包結構下。
內容:<?xml version="1.0" encoding="utf-8"?>
username must 6-8位!
user.password">
requiredstring">
true
password must is not null!
stringlength">
true
minlength">6
maxlength">6
password must 6位!
user.birthday">
date">
1980-01-01
2010-01-01
birthday is date and must is 1980-01-01 to 2010-01-01
strut2提供的驗證規則還有:
<
validators
>
<
validator
name
="required"
class
="com.opensymphony.xwork2.validator.validators.requiredfieldvalidator"
/>
<
validator
name
="requiredstring"
class
="com.opensymphony.xwork2.validator.validators.requiredstringvalidator"
/>
<
validator
name
="int"
class
="com.opensymphony.xwork2.validator.validators.intrangefieldvalidator"
/>
<
validator
name
="double"
class
="com.opensymphony.xwork2.validator.validators.doublerangefieldvalidator"
/>
<
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"
/>
<
validator
name
="regex"
class
="com.opensymphony.xwork2.validator.validators.regexfieldvalidator"
/>
validators
>
Struts2驗證框架
action配置中一定要設定input返回頁面 新增驗證只要建立驗證的xml檔案 在action同包下,建立 action類名 validation.xml 如 validateaction建立validateaction validation.xml 注意 1.要驗證的方法不能叫input.2.這...
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...