第一种:继承ActionForm的Form
表单验证在Form中
在xml中的配置如下:
<form-bean name="testForm" type="com.yourcompany.struts.form.TestForm" />
类的写法如下:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.yourcompany.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
/**
* MyEclipse Struts Creation date: 09-08-2008
*
* XDoclet definition:
*
* @struts.form name="testForm"
*/
public class TestForm extends ActionForm {
/*
* Generated Methods
*/
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
/**
* Method validate
*
* @param mapping
* @param request
* @return ActionErrors
*/
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = new ActionErrors();
if (userName.trim().length() == 0) {
errors.add("userName.empty", new ActionMessage("userName.empty"));
}
if (password.trim().length() == 0) {
errors.add("password.empty", new ActionMessage("password.empty"));
}
return errors;
}
/**
* Method reset
*
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
userName = "";
password = "";
}
}
第二种:继承ValidatorAction的Form
表单验证在validation.xml中
配置和第一种一样
类的写法:吧原有的alidate方法去掉
在validaion.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>
<formset>
<form name="testForm">
<field property="userName" depends="required">
<arg0 key="userName.empty" />
<!-- The key is the key in file ApplicationResources.properties-->
</field>
<field property="password" depends="required">
<arg0 key="password.empty" />
</field>
</form>
</formset>
</form-validation>
第三种:配置DynaActionForm的Form
没有验证功能
在struts-config.xml中的配置:
<form-bean name="testForm" type="com.yourcompany.struts.form.DynaActionForm">
<form-property name="userName" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
</form-bean>
用这个类就不再要写Form了,只要像上面那样的配置下就行了,方便吧!
第四种:配置DynaValidatorForm的Form
验证写在validation.xml中
在struts-config.xml中的配置:
<form-bean name="testForm" type="com.yourcompany.struts.form.DynaValidatorForm">
<form-property name="userName" type="java.lang.String"></form-property>
<form-property name="password" type="java.lang.String"></form-property>
</form-bean>
这个一样不用写类了,但要配置,在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>
<formset>
<form name="testForm">
<field property="userName" depends="required">
<arg0 key="userName.empty" />
<!-- The key is the key in file ApplicationResources.properties-->
</field>
<field property="password" depends="required">
<arg0 key="password.empty" />
</field>
</form>
</formset>
</form-validation>
上面这些是我们开发中常用的,还有Lazy Form,这个我们以后再说吧。