Struts使用Validator
Validator框架是一种可插拔的系统,validator-rules.xml文件用于以声明的方式插入执行验证时validator将要使用的验证例程。
启用Validator插件
struts-config.xml文件里加入
<!-- =================================================== Validator plugin -->
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property
property="pathnames"
value="/WEB-INF/config/validator-rules.xml,/WEB-INF/config/validation.xml"/>
</plug-in>
配置validator-rules.xml
<form-validation>
<global>
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>
<validator name="mask"
classname="org.apache.struts.validator.FieldChecks"
method="validateMask"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
javax.servlet.http.HttpServletRequest"
depends=""
msg="errors.invalid"/>
</global>
</form-validation>
创建FormBean
要使用Validator,应用程序的Form Bean必须是Validator的ActionFormde的子类而不是使用ActionForm类本身。
/*
* Created on 2005/06/18
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.nova.colimas.web.form;
import org.apache.struts.validator.ValidatorForm;
/**
* @author tyrone
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class LoginForm extends ValidatorForm {
private String password;
private String userID;
public void Reset() {
this.password="";
this.userID="";
return;
}
/**
* @return Returns the UserID.
*/
public String getUserID() {
return userID;
}
/**
* @param UserID The UserID to set.
*/
public void setUserID(String property1) {
this.userID = property1;
}
/**
* @return Returns the Password.
*/
public String getPassword() {
return password;
}
/**
* @param Password The Password to set.
*/
public void setPassword(String property1) {
this.password = property1;
}
}
struts-config.xml里声明FormBean
<!--2 Login formbean-->
<form-bean
name="LoginForm"
type="com.nova.colimas.web.form.LoginForm"/>
配置validator.xml文件
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!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>
<!--
This is a minimal Validator form file with a couple of examples.
-->
<formset>
<form name="LoginForm">
<field
property="userID"
depends="required">
<arg0 key="LoginForm.userid"/>
</field>
<field
property="password"
depends="required,mask">
<arg0 key="LoginForm.password"/>
<var>
<var-name>mask</var-name>
<var-value>^[0-9a-zA-Z]*$</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
配置资源文件
struts-config.xml
<message-resources parameter="resources.config.index" />
index.properties
#error
errors.invalid={0} is invalid.
errors.required={0} is required.
#key
LoginForm.userid=User ID
LoginForm.password=Password
启动验证Index.jsp
<html:form action="/LoginAction.do">
<p><span class="style15"><bean:message key="index.jsp.userid" /></span><input
type="text" name="userID/><br>
<span class="style15"><bean:message key="index.jsp.pass" /></span><input type="password" name="password"/><br>
<html:submit>
<bean:message key="index.jsp.login" />
</html:submit></p>
<html:errors />
</html:form>
验证出错后,返回action的input属性值的页面,错误显示在<html:errors />位置。
例如struts-config.xml如下,则会显示在"/pages/index.jsp"
<action path="/LoginAction"
type="com.nova.colimas.web.action.LoginAction"
name="LoginForm"
scope="session"
input="/pages/index.jsp"
validate="true">
<forward name="success" path="/pages/index.jsp"/>
<forward name="failure" path="/pages/index.jsp"/>
</action>