在服务器端进行异常处理:
1.各种类型的异常类:
publicclass ValidateException extends RuntimeException {
public ValidateException() {}
public ValidateException(String msg) {
super(msg);
}
public ValidateException(int msgId) {
super();
}
}
publicclass CreateException extends RuntimeException {
public CreateException() {}
public CreateException(String msg) {
super(msg);
}
public CreateException(Throwable throwable) {
super(throwable);
}
}
2.定义VO中的验证接口及方法:
publicinterface Validator {
staticfinalintINVALID_ACCOUNT_USERNAME = 1000; //"Invalid username: must 6-12 characters.";
staticfinalintINVALID_ACCOUNT_PASSWORD = 1001;
staticfinalintAPPLICATION_ERROR = 909091;
void validate() throws ValidateException;
}
publicclass Account implements Validator {
publicstaticfinalintSTATE_ACTIVE = 0;
publicstaticfinalintSTATE_INACTIVE = 1;
publicstaticfinalintSTATE_LOCKED = 2;
// blogger login info:
privateintaccountId;
private String username;
private String password;
private String email = "";
private Date createdDate;
// personal setting:
。。。。。。。。。。。。。。
publicvoid validate() throws ValidateException {
if(username==null || username.equals(""))
thrownew ValidateException("用户名不能为空");
// if(!username.matches("[a-z0-9]{6,20}"))
// throw new ValidateException(INVALID_ACCOUNT_USERNAME);
if(password==null || password.equals(""))
thrownew ValidateException(“密码不能为空”);
}
}
3.sturts中公共的处理类:
publicclass AppExceptionHandler extends ExceptionHandler {
/**
*Loggerforthisclass
*/
privatestaticfinal Log logger = LogFactory
.getLog(AppExceptionHandler.class);
public ActionForward execute(Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response) throws ServletException {
ActionForward forward = null;
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
String url = request.getRequestURL().toString();
String param = request.getQueryString();
if(param!=null && !param.equals(""))
url = url + "?" + param;
String message =ex.getMessage();
request.setAttribute("url", url);
request.setAttribute("message", message);
return forward;
}
}
4.Struts-config.xml中的配置:
<global-exceptions>
<exception
key="error.sysError"
type="java.lang.Exception"
handler="com.yourcompany.struts.action.AppExceptionHandler"
path="/error.jsp"/>
</global-exceptions>
5.Error.jsp页面内容:
<%@ page contentType="text/html;charset=GBK" %>
<%@ taglib uri="/WEB-INF/c.tld" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ERROR</title>
<link href="normal.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#FFFFFF">
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><table width="570" border="01" cellpadding="03" cellspacing="0" bordercolor="#A5B6D1">
<tr>
<td bgcolor="#A5B6D1">Error</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
错误信息:</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
<b>请求URL</b>: <c:out value="${url}"/><br/>
<b>错误原因</b>: <c:out value="${message}"/>
</td>
</tr>
<tr>
<td bordercolor="#FFFFFF">
Please go back to the previous page and check your input.
Also you can contact the <a href="mailto:asklxf@163.com">author</a> if you are sure this is a bug of the system. Thank you!
</td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
注意事项:<%@ page errorPage="/common/errorpage.jsp" %>
表示如果是页面有错误就会转到errorpage.jsp中去。
同时errorpage.jsp中要声明<%@ page isErrorPage="true" %>