在上一篇文章中我们知道,Struts1.x通过ActionForm的子类来封装了客户端提交的form中的数据。而服务端程序只需要通过ActionForm的子类的对象实例就可以访问form中的数据,而如果不使用ActionForm类,就必须通过request对象来获得form中的数据。通过这种封装机制可以使代码更容易理解。然而,ActionForm类不仅可以封装form中的数据,还可以通过ActionForm类的validate方法来验证form中的数据。validate方法的定义如下:
【第1步】建立JSP页面
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<head>
<title>注册信息(测试简单验证)</title>
<style type="text/css">
.text {
height: 20px;
width: 160px;
}
</style>
</head>
<body>
<html:form action="simpleValidation">
<table width="100%">
<tr>
<td align="right" width="45%"> 用户名:</td>
<td width="55%">
<html:text property="user" styleClass="text" />
<font color="red"><html:errors property="errorUser" /></font>
</td>
</tr><tr /><tr />
<tr>
<td align="right">登录密码:</td>
<td>
<html:password property="password" styleClass="text" />
<font color="red"><html:errors property="errorPassword" /></font>
</td>
</tr><tr /><tr />
<tr>
<td align="right">重复登录密码:</td>
<td>
<html:password property="password1" styleClass="text" />
<font color="red"><html:errors property="errorPassword1" /></font>
</td>
</tr><tr /><tr />
<tr>
<td align="right">电子邮件:</td>
<td>
<html:text property="email" styleClass="text" />
<font color="red"><html:errors property="errorEmail" /></font>
</td>
</tr><tr /><tr />
<tr>
<td align="right"> <br> ${requestScope.success } </td>
<td align="left"> <br> <html:submit value=" 提交 " /> </td>
</tr>
</table>
</html:form>
</body>
</html>
【第2步】建立simpleValidation动作
import javax.servlet.http.*;
import org.apache.struts.action.*;
public class SimpleValidationAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
request.setAttribute("success", "提交成功!"); // 设置在目标页中显示的信息字符串
return mapping.findForward("simple"); // 跳转到目录页(simple所指的JSP页)
}
}
在这一步我们来建立一个用于接收有户的提交信息的ActionForm类:SimpleValidationForm。这个类从org.apache.struts.action.ActionForm类继承。在<samples工程目录>"src"actionform目录中建立一个SimpleValidationForm.java文件,代码如下:
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public class SimpleValidationForm extends ActionForm
{
// 以下四个变量分别对应于simpleValidation.jsp中的四个文本框中的值。
private String user;
private String password;
private String password1;
private String email;
public String getUser()
{
return user;
}
public void setUser(String user)
{
this.user = user;
}
public String getPassword()
{
return password;
}
public void setPassword(String password)
{
this.password = password;
}
public String getPassword1()
{
return password1;
}
public void setPassword1(String password1)
{
this.password1 = password1;
}
public String getEmail()
{
return email;
}
public void setEmail(String email)
{
this.email = email;
}
// 开始验证用户提交的信息
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors error = new ActionErrors();
if (user.equals("")) // 必须输入用户名
error.add("errorUser", new ActionMessage("error.user.blank"));
if (password.equals("")) // 必须输入密码
error.add("errorPassword", new ActionMessage("error.password.blank"));
else if (!password.equals(password1)) // 两个登录密码必须一致
error.add("errorPassword1", new ActionMessage("error.password1.confirmation"));
if (email.equals("")) // 必须输入email
error.add("errorEmail", new ActionMessage("error.email.blank"));
else if (!email.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+")) // 验证email的格式是否正确
error.add("errorEmail", new ActionMessage("error.email.invalid"));
// 返回错误信息,如果error中没有错误信息,
// 就会调用SimpleValidationAction类的对象实例来执行execute方法。
return error;
}
}
1. 要想在ActionForm类中进行验证,必须在ActionForm类的子类中覆盖validate方法。
2. validate方法在ActionForm类的对象实例装载完用户提交的数据后调用,因此,在调用validate方法时,ActionForm类的属性值已经是用户提交的信息了。所以可以直接使用这些属性值进行验证。
3. 在validate方法中验证用户提交的数据时,要使用ActionErrors类的实例对象返回错误信息
4. ActionErrors类的构造方法的第二个参是一个ActionMessage类的对象实例,而不是错误描述信息。
5.ActionMessage类的构造方法的参数并不是错误描述信息,而是错误描述信息的key,具体的信息在Java属性文件中(将在下一步实现)。
6. 使用ActionForm的属性可以非常好地验证字符串类型,但对于其他的数据类型(如整型)的某些验证却不太适合。如当用户提交数据时,本该提交一个整数,但用户却提交了一个非整数信息。对于这种情况,在ActionForm类的对象实例中这个用户提交的数据的值为0。虽然使用ActionForm类的属性无法准确验证这种情况,但我们可以使用validate方法的第二个参数request的getParameter方法直接获得客户端提交的数据来进行验证。
7. 如果ActionErrors对象中有错误信息,在JSP中需要使用<html:errors>标签显示错误信息。
8. Struts实际上是将ActionErrors对象以org.apache.struts.action.ERROR作为键值保存在了request的属性中。因此,<html:errors>标签实际上是从request的属性中获得的错误信息描述。如我们也可以通过如下的Java代码来获得produceID属性的错误描述信息:
java.util.Iterator<org.apache.struts.action.ActionMessage> it =
((org.apache.struts.action.ActionErrors)request
.getAttribute("org.apache.struts.action.ERROR")).get("productID");
out.println(((org.apache.struts.util.PropertyMessageResources )request
.getAttribute("org.apache.struts.action.MESSAGE")).getMessage("error.productID.blank",null));
%>
ErrorDescription.properties
error.password.blank = Password can't be null.
error.password1.confirmation = Password doesn't match confirmation.
error.email.blank = Email can't be null.
error.email.invalid = It is not a valid email address.
在本例中需要配置struts-config.xml文件的三个标签:<form-bean>、<action>和<message-resources>。
1. 配置<form-bean>标签
这个标签用来定义Struts中的动作类。在<action-mappings>标签中加入如下所示的<action>标签:
input="simpleValidation.jsp">
<forward name="simple" path="simpleValidation.jsp" />
</action>
下面我们测试一下这个例子程序。首先启动Tomcat,然后在IE中输入如下的URL:
http://localhost:8080/samples/simpleValidation.jsp

图1

图2

















