第一步,在Struts-config.xml中配置一个ActionForm,示例如下:
<form-bean
name="checkoutForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="firstName" type="java.lang.String"/>
<form-property name="lastName" type="java.lang.String"/>
<form-property name="address" type="java.lang.String"/>
<form-property name="city" type="java.lang.String"/>
<form-property name="state" type="java.lang.String"/>
<form-property name="postalCode" type="java.lang.String"/>
<form-property name="country" type="java.lang.String"/>
<form-property name="phone" type="java.lang.String"/>
</form-bean>
第二步,在Struts-config.xml中配置一个Action,示例如下:
<action
input="/checkout.jsp"
name="checkoutForm"
path="/checkout"
scope="request"
type="com.ort.struts.example.checkOutAction"
validate="true">
</action>
第三布,在validation.xml文件中定义如下:
<formset>
<constant>
<constant-name>phone</constant-name>
<constant-value>^\(?(\d{3})\)?[-| ]?(\d{3})[-| ]?(\d{4})$</constant-value>
</constant>
<constant>
<constant-name>zip</constant-name>
<constant-value>^\d{5}(-\d{4})?$</constant-value>
</constant>
<form name="checkoutForm">
<field
property="firstName"
depends="required,mask">
<arg0 key="label.firstName"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]*$</var-value>
</var>
</field>
<field
property="postalCode"
depends="required,mask">
<arg0 key="registrationForm.zip"/>
<var>
<var-name>mask</var-name>
<var-value>${zip}</var-value>
</var>
</field>
<field
property="phone"
depends="required,mask">
<arg0 key="registrationForm.phone"/>
<var>
<var-name>mask</var-name>
<var-value>${phone}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
第四部,编写HTML页面如下:
暂略
本文地址:http://com.8s8s.com/it/it16586.htm