public class SystemException extends RuntimeException{ private String errid; private String[] msgs; public String getErrid(){ return errid; } public String[] getMsgs(){ return msgs; } public SystemException(String pErrid, String[] pMsgs){ super(pErrid); errid = pErrid; msgs = pMsgs; } public SystemException(String pErrid, String[] pMsgs, Throwable cause){ super(pErrid,cause); errid = pErrid; msgs = pMsgs; } public SystemException(String pErrid, String msg1){ SystemException(pErrid, new String[]{msg1}); } public SystemException(String pErrid, String msg1, Throwable cause){ SystemException(pErrid, new String[]{msg1}, cause); } …(以下msg2,msg3,msg4,...をとれるコンストラクタがつづく) }
public class SystemExceptionHandler extends ExceptionHandler{ /** commons-logging */ private static Log logger = LogFactory.getLog(SystemExceptionHandler.class); public ActionForward execute( Exception ex, ExceptionConfig ae, ActionMapping mapping, ActionForm formInstance, HttpServletRequest request, HttpServletResponse response) throws ServletException { 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 if (ex instanceof SystemException) { * error = new ActionMessage(((SystemException) ex).getErrId(), ((SystemException) ex).getMsgs()); * property = error.getKey(); } else { error = new ActionMessage(ae.getKey(), ex.getMessage()); 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; } }
<global-exceptions> <exception type="com.foo.SystemException" path="/syserr.jsp" handler="com.foo.SystemExceptionHandler" /> </global-exception>
class OrderDAO { public OrderVO findByPK( String orderNo ){ ..... throw new SystemException("syserr.duplicate","ORDER_TBL",orderNo); ..... } }
<%@ page contentType="text/html;charset="Shift_JIS" language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html:html> <head><title>System Error</title> <body> <h1>申し訳ありません、システムエラーが発生しました</h1> システム管理課にご連絡下さい(内線番号 xxxx-xxxx)<br/> <br/> <html:errors> </body> </html:html>
syserr.duplicatie=プライマリキーが重複しています。テーブル名:{0} , 登録キー:{1}
public class SystemExceptionConfig extends ExceptionConfig{ protected String errLevel; public String getErrLevel(){ return errLevel; } public void setErrLevel( String param ){ errLevel = param; } }
<global-exceptions> <exception type="com.foo.SystemException" path="/syserr.jsp" handler="com.foo.SystemExceptionHandler" classname="com.foo.SystemExceptionConfig"> <set-property property="errLevel" value="fatal" /> </excetion> </global-exception>
public class SystemExceptionHandler extends ExceptionHandler{ /** commons-logging */ private static Log logger = LogFactory.getLog(SystemExceptionHandler.class); public ActionForward execute(Exception ex, ExceptionConfig ec, ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws ServletException{ long errRefId = System.currentTimeMillis(); logMsg = "System Error Occured(#" + errRefId + ")" SystemExceptionConfig conf = (SystemExceptionConfig)ec; if( "fatal".equals( conf.getErrLevel() ) ){ logger.fatal(logMsg, ex); }else if("warn".equals( conf.getErrLevel() ) ){ logger.warn(logMsg, ex); (以下略)