Anyframe에서 공통으로 사용하는 BaseException을 포함한 Exception이 throw 되었을 때, 이를 처리하는 ExceptionHandler에 대해 알아본다.
<action name="userForm" path="/empLogin" type="com.sds.emp.user.web.LogInAction" scope="request" validate="false"> <exception handler="anyframe.web.struts.common.util.DefaultBaseExceptionHandler" type="anyframe.common.exception.BaseException" key="common.msg.global.error"/> <forward name="success" path="/home.do" /> <forward name="failure" path="/empLoginView.do" /> </action>
<global-exceptions>
<exception
path="/sample/common/error.jsp"
key="error.common.msg.authentication"
type="anyframe.web.struts.common.util.AuthenticationException"
handler="com.sds.emp.common.EmpExceptionHandler" />
<exception
path="/sample/common/error.jsp"
key="error.common.msg.authorization"
type="anyframe.web.struts.common.util.AuthorizationException"
handler="com.sds.emp.common.EmpExceptionHandler" />
<exception
path="/sample/common/error.jsp"
key="common.msg.invalidtoken.error"
type="anyframe.web.struts.common.util.InvalidTokenException"
handler="com.sds.emp.common.EmpExceptionHandler" />
<exception key="error.common.msg"
path="/sample/common/error.jsp"
type="com.sds.emp.common.EmpException"
handler="com.sds.emp.common.EmpExceptionHandler" />
<exception key="error.common.msg"
path="/sample/common/error.jsp"
type="java.lang.Exception"
handler="com.sds.emp.common.EmpExceptionHandler" />
</global-exceptions>
protected String defaultBundle = "anyframe.web.struts.common.CommonResource";
..
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();
}
try {
if (ex instanceof BaseException) {
BaseException baseEx = (BaseException) ex;
request.setAttribute(Globals.EXCEPTION_KEY, ex);
logException("emp exception is occurred! ", ex);
String[] messages = new String[2];
messages[0] = baseEx.getMessages().getUserMessage();
messages[1] = baseEx.getMessages().getSolution();
storeException(request, messages, forward, ae.getScope());
} else {
request.setAttribute(Globals.EXCEPTION_KEY, ex);
logException("runtime exception is occurred! ", ex);
MessageResourcesFactory factory = MessageResourcesFactory.createFactory();
MessageResources messageResources = factory.createResources(defaultBundle);
if(ex instanceof ModuleException) {
ModuleException moduleEx = (ModuleException) ex;
ActionMessage actionMessage = moduleEx.getActionMessage();
String messageKey = actionMessage.getKey();
String[] messages = new String[2];
String[] args = {""};
messages[0] = messageResources.getMessage(Locale.getDefault(),
messageKey, args);
messages[1] = messageResources
.getMessage(Locale.getDefault(), messageKey + ".solution", args);
storeException(request, messages, forward, ae.getScope());
} else {
String[] messages = new String[3];
String[] args = {""};
messages[0] = messageResources.getMessage(Locale.getDefault(), ae.getKey(), args);
messages[1] = ""; // solution is empty
messages[2] = ex.getMessage(); // reason is exception message
storeException(request, messages, forward, ae.getScope());
}
}
… 하략 …
public class EmpExceptionHandler extends DefaultBaseExceptionHandler {
public EmpExceptionHandler() {
this.defaultBundle = "com.sds.emp.EmpResources";
}
public ActionForward execute(Exception exception, ExceptionConfig config,
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws ServletException {
ActionForward forward = mapping.getInputForward();
String forwardPath = forward.getPath();
if (forwardPath == null || forwardPath.equals("")) {
request.setAttribute("authFail", "true");
}
String url = "/empLoginView.do" + "?";
Enumeration enumrequest = request.getParameterNames();
while (enumrequest.hasMoreElements()) {
String parameterName = (String) enumrequest.nextElement();
String parameterValue = request.getParameter(parameterName);
url += parameterName + "=" + parameterValue + "&";
}
forward.setPath(url);
request.getSession().setAttribute("afterErrorPage", forward);
return super.execute(exception, config, mapping, form, request, response);
}
}
<%
...
String[] messages = (String[])request.getAttribute(Globals.MESSAGE_KEY);
String userMessage = messages[0];
String solution = "";
String reason = "";
if(messages.length==2) {
solution = messages[1];
}
if(messages.length==3) {
solution = messages[1];
reason = messages[2];
}
%>
...
<%= userMessage %><p/>
<% if(!solution.equals("")) { %>
<strong>* SOLUTION</strong><br/>
<%= solution %>
<% } %>
<% if(!reason.equals("")) { %>
<strong>* REASON</strong><br/>
<%= reason %>
<% } %>
...
<td background="<html:rewrite page='/sample/images/ct_btnbg02.gif'/>"
class="ct_btn01" style="padding-top:3px;">
<a href="javascript:fncGoAfterErrorPage();">확인</a>
</td>
...