struts异常处理

struts-config.xml

<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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值