struts-config.xml
覆盖ExceptionHandler
<global-exceptions>
<exception key="" type="com.hugui.struts.ErrorCodeException" path="/error.jsp" handler="com.hugui.struts.ErrorCodeExceptionHandler"></exception>
</global-exceptions>
覆盖ExceptionHandler
package com.hugui.struts;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ExceptionHandler;
import org.apache.struts.config.ExceptionConfig;
import org.apache.struts.util.ModuleException;
/**
* 个性化异常处理类
* @author Administrator
*
*/
public class ErrorCodeExceptionHandler extends ExceptionHandler {
public ActionForward execute(
Exception ex,
ExceptionConfig ae,
ActionMapping mapping,
ActionForm formInstance,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException {
if(!(ex instanceof ErrorCodeException)){
return super.execute(ex, ae, mapping, formInstance, request, response);
}
ActionForward forward = null;
ActionMessage error = null;
String property = null;
// Build the forward from the exception mapping if it exists
// or from the form input
if (ae.getPath() != null) {
forward = new ActionForward(ae.getPath());
} else {
forward = mapping.getInputForward();
}
// Figure out the error
if (ex instanceof ModuleException) {
error = ((ModuleException) ex).getActionMessage();
property = ((ModuleException) ex).getProperty();
} else {
// error = new ActionMessage(ae.getKey(), ex.getMessage());
ErrorCodeException ece = (ErrorCodeException)ex;
error = new ActionMessage(ece.getErrorCode(), ece.getArgs()); property = error.getKey();
}
this.logException(ex);
// Store the exception
request.setAttribute(Globals.EXCEPTION_KEY, ex);
this.storeException(request, property, error, forward, ae.getScope());
return forward;
}
}
package com.hugui.struts;
public class AppException extends RuntimeException {
public AppException() {
// TODO Auto-generated constructor stub
}
public AppException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public AppException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public AppException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
}
package com.hugui.struts;
public class UserManager {
public void login(String username, String password){
if(!"admin".equals(username)){
throw new ErrorCodeException("login.user.not.found",username);
}
if(!"admin".equals(password)){
throw new ErrorCodeException("login.password.error");
}
}
}
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<html:errors/>
</body>
</html>