1.checkInfo.jsp
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
</head>
<body>
<html:form action="/check">
姓名:<html:text property="name" /><html:errors property="name"/><br>
街道:<html:text property="street"/><html:errors property="street"/><br>
城市:<html:text property="city"/><html:errors property="city"/><br>
省名:<html:text property="province"/><html:errors property="province"/><br>
邮编:<html:text property="postCode"/><html:errors property="postCode"/><br>
国家:<html:text property="country"/><html:errors property="country"/><br>
电话:<html:text property="phone"/><html:errors property="phone"/><br>
<html:submit>提交</html:submit>
<html:reset>重置</html:reset>
</html:form>
</body>
</html:html>
2.checkForm.java
package com.yourcompany.struts.form;
import org.apache.struts.validator.ValidatorActionForm;
public class checkForm extends ValidatorActionForm{
private String name;
private String street;
private String city;
private String province;
private String postCode;
private String country;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans >
<form-bean name="checkForm" type="com.yourcompany.struts.form.CheckForm" />
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings >
<action
attribute="checkForm"
name="checkForm"
input="/JSP/checkInfo.jsp"
path="/check"
scope="request"
validate="true"
type="com.yourcompany.struts.action.CheckAction">
<forward name="success" path="/JSP/ok.jsp" />
</action>
</action-mappings>
<message-resources parameter="com.yourcompany.struts.ApplicationResources" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
4.CheckAction.java
package com.yourcompany.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class CheckAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response) {
return mapping.findForward("success");
}
}
5.validation.xml
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.1.3//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1_3.dtd">
<form-validation>
<global>
<constant>
<constant-name>phone</constant-name>
<constant-value>^\d{8}\d*$</constant-value>
</constant>
</global>
<formset>
<form name="checkForm">
<field property="name" depends="required">
<arg0 key="label.name"/>
</field>
<field property="street" depends="required">
<arg0 key="label.street"/>
</field>
<field property="city" depends="required">
<arg0 key="label.city"/>
</field>
<field property="province" depends="required">
<arg0 key="label.province"/>
</field>
<field property="postCode" depends="required">
<arg0 key="label.postCode"/>
</field>
<field property="country" depends="required">
<arg0 key="label.country"/>
</field>
<field property="phone" depends="required">
<arg0 key="label.phone"/>
</field>
<field property="phone" depends="minlength">
<arg0 key="label.phone" />
<arg1 name="minlength" key="${var:minlength}" resource="false"/>
<var>
<var-name>minlength</var-name>
<var-value>4</var-value>
</var>
</field>
<field property="phone" depends="mask">
<arg0 key="label.phone" />
<msg name="mask" key="must eight numbers" resource="false"/>
<var>
<var-name>mask</var-name>
<var-value>${phone}</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
6.ApplicationResources.properties
# Resources for parameter 'com.yourcompany.struts.ApplicationResources'
label.name=<b>name</b>
label.street=<b>street</b>
label.city=<b>city</b>
label.province=<b>province</b>
label.postCode=<b>postCode</b>
label.country=<b>country</b>
label.phone=<b>phone</b>
errors.required={0} is required.
errors.minlength={0} can not be less than {1} characters.
errors.maxlength={0} can not be greater than {1} characters.
errors.invalid={0} is invalid.
errors.byte={0} must be a byte.
errors.short={0} must be a short.
errors.integer={0} must be an integer.
errors.long={0} must be a long.
errors.float={0} must be a float.
errors.double={0} must be a double.
errors.date={0} is not a date.
errors.range={0} is not in the range {1} through {2}.
errors.creditcard={0} is an invalid credit card number.
errors.email={0} is an invalid e-mail address.