日期:2014-05-20  浏览次数:20727 次

请能发个 表单的 服务器端验证
我不知道怎么弄actionForm 的validate()怎么写,它里面怎么会有一个ActionError 类对呢;我找不到呀?
请能弄个简单的实例过来? 50分

------解决方案--------------------
//ActionForm
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
//一个用来装在验证过程中出现的错误的容器
ActionErrors errors = new ActionErrors();
try {
if (pwd.length()<6){
//如果密码长度不够,往errors中放入一个错误
//add()方法的第一个参数给的是jsp页面里的错误标识,><html:errors property="pwderror"/>
errors.add("pwderror",new ActionMessage("pwd.length.error"));
}

if (email.indexOf("@")==-1){
errors.add("emailformaterror",new ActionMessage("invail.eamil.format"));
}

} catch (Exception e) {
e.printStackTrace();
}
return errors;

//jsp页面
<%@ page language="java" pageEncoding="GB18030"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
 
<html> 
<head>
<title>JSP for RegForm form</title>
</head>
<body>
<html:form action="/reg">
email : <html:text property="email"/><html:errors property="emailformaterror"/><br/>
pwd : <html:text property="pwd"/><html:errors property="pwderror"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>

------解决方案--------------------
ActionErrors 它就是装错误信息的容器。
它里面的add()方法存放的错误信息是以键值对的形式。