步骤一、创建如下validate.jsp页面:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<h3>All Field Errors Will Appear Here</h3>
<s:fielderror />
<hr/>
<h3>Field Error due to 'Required String Validator Field' Will Appear Here</h3>
<s:fielderror>
<s:param value="%{'requiredStringValidatorField'}" />
</s:fielderror>
<hr/>
<h3>Field Error due to 'String Length Validator Field' Will Appear Here</h3>
<s:fielderror>
<s:param>stringLengthValidatorField</s:param>
</s:fielderror>
<hr/>
<s:form action="validate.action" namespace="/validation" method="POST" theme="xhtml">
<s:textfield label="Required Validator Field" name="requiredValidatorField" />
<s:textfield label="Required String Validator Field" name="requiredStringValidatorField" />
<s:textfield label="Integer Validator Field" name="integerValidatorField" />
<s:textfield label="Date Validator Field" name="dateValidatorField" />
<s:textfield label="Email Validator Field" name="emailValidatorField" />
<s:textfield label="URL Validator Field" name="urlValidatorField" />
<s:textfield label="String Length Validator Field" name="stringLengthValidatorField" />
<s:textfield label="Regex Validator Field" name="regexValidatorField"/>
<s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField" />
<s:submit label="Submit" />
</s:form>
步骤二、建立如下的Action类:
package web.actions;
import java.util.Date;
import com.opensymphony.xwork2.ActionSupport;
public class FieldValidatorsExampleAction extends ActionSupport {
private static final long serialVersionUID = -4829381083003175423L;
private String requiredValidatorField = null;
private String requiredStringValidatorField = null;
private Integer integerValidatorField = null;
private Date dateValidatorField = null;
private String emailValidatorField = null;
private String urlValidatorField = null;
private String stringLengthValidatorField = null;
private String regexValidatorField = null;
private String fieldExpressionValidatorField = null;
public Date getDateValidatorField() {
return dateValidatorField;
}
public void setDateValidatorField(Date dateValidatorField) {
this.dateValidatorField = dateValidatorField;
}
public String getEmailValidatorField() {
return emailValidatorField;
}
public void setEmailValidatorField(String emailValidatorField) {
this.emailValidatorField = emailValidatorField;
}
public Integer getIntegerValidatorField() {
return integerValidatorField;
}
public void setIntegerValidatorField(Integer integerValidatorField) {
this.integerValidatorField = integerValidatorField;
}
public String getRegexValidatorField() {
return regexValidatorField;
}
public void setRegexValidatorField(String regexValidatorField) {
this.regexValidatorField = regexValidatorField;
}
public String getRequiredStringValidatorField() {
return requiredStringValidatorField;
}
public void setRequiredStringValidatorField(String requiredStringValidatorField) {
this.requiredStringValidatorField = requiredStringValidatorField;
}
public String getRequiredValidatorField() {
return requiredValidatorField;
}
public void setRequiredValidatorField(String requiredValidatorField) {
this.requiredValidatorField = requiredValidatorField;
}
public String getStringLengthValidatorField() {
return stringLengthValidatorField;
}
public void setStringLengthValidatorField(String stringLengthValidatorField) {
this.stringLengthValidatorField = stringLengthValidatorField;
}
public String getFieldExpressionValidatorField() {
return fieldExpressionValidatorField;
}
public void setFieldExpressionValidatorField(
String fieldExpressionValidatorField) {
this.fieldExpressionValidatorField = fieldExpressionValidatorField;
}
public String getUrlValidatorField() {
return urlValidatorField;
}
public void setUrlValidatorField(String urlValidatorField) {
this.urlValidatorField = urlValidatorField;
}
}
步骤三、建立FieldValidatorsExampleAction-validation.xml文件,并与FieldValidatorsExampleAction类置于同一个文件夹下:
<!DOCTYPE validators PUBLIC
"-//OpenSymphony Group//XWork Validator 1.0.2//EN"
"http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
<validators>
<field name="requiredValidatorField">
<field-validator type="required">
<message><![CDATA[ required ]]></message>
</field-validator>
</field>
<field name="requiredStringValidatorField">
<field-validator type="requiredstring">
<param name="trim">true</param>
<message><![CDATA[ required and must be string ]]></message>
</field-validator>
</field>
<field name="integerValidatorField">
<field-validator type="int">
<param name="min">1</param>
<param name="max">10</param>
<message><![CDATA[ must be integer min 1 max 10 if supplied ]]></message>
</field-validator>
</field>
<field name="dateValidatorField">
<field-validator type="date">
<param name="min">01/01/1990</param>
<param name="max">01/01/2000</param>
<message><![CDATA[ must be a min 01-01-1990 max 01-01-2000 if supplied ]]></message>
</field-validator>
</field>
<field name="emailValidatorField">
<field-validator type="email">
<message><![CDATA[ must be a valid email if supplied ]]></message>
</field-validator>
</field>
<field name="urlValidatorField">
<field-validator type="url">
<message><![CDATA[ must be a valid url if supplied ]]></message>
</field-validator>
</field>
<field name="stringLengthValidatorField">
<field-validator type="stringlength">
<param name="maxLength">4</param>
<param name="minLength">2</param>
<param name="trim">true</param>
<message><![CDATA[ must be a String of a specific greater than 1 less than 5 if specified ]]></message>
</field-validator>
</field>
<field name="regexValidatorField">
<field-validator type="regex">
<param name="expression">.*\.txt</param>
<message><![CDATA[ regexValidatorField must match a regexp (.*\.txt) if specified ]]></message>
</field-validator>
</field>
<field name="fieldExpressionValidatorField">
<field-validator type="fieldexpression">
<param name="expression">(fieldExpressionValidatorField == requiredValidatorField)</param>
<message><![CDATA[ must be the same as the Required Validator Field if specified ]]></message>
</field-validator>
</field>
</validators>
步骤四、配置struts.xml,加入新的action映射:
<action name="validate" class="web.actions.FieldValidatorsExampleAction">
<result name="success">/showinfo.jsp</result>
<result name="input">/validate.jsp</result>
</action>
posted on 2007-11-13 22:53
huangzhiwei 阅读(1722)
评论(0) 编辑 收藏