1 18 19 package org.apache.struts.action; 20 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServletRequest ; 23 import javax.servlet.http.HttpServletResponse ; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.struts.Globals; 28 import org.apache.struts.config.ExceptionConfig; 29 import org.apache.struts.util.MessageResources; 30 import org.apache.struts.util.ModuleException; 31 32 39 public class ExceptionHandler { 40 41 42 45 private static final Log log = LogFactory.getLog(ExceptionHandler.class); 46 47 48 51 private static MessageResources messages = 52 MessageResources.getMessageResources( 53 "org.apache.struts.action.LocalStrings"); 54 55 56 72 public ActionForward execute( 73 Exception ex, 74 ExceptionConfig ae, 75 ActionMapping mapping, 76 ActionForm formInstance, 77 HttpServletRequest request, 78 HttpServletResponse response) 79 throws ServletException { 80 81 ActionForward forward = null; 82 ActionMessage error = null; 83 String property = null; 84 85 if (ae.getPath() != null) { 88 forward = new ActionForward(ae.getPath()); 89 } else { 90 forward = mapping.getInputForward(); 91 } 92 93 if (ex instanceof ModuleException) { 95 error = ((ModuleException) ex).getActionMessage(); 96 property = ((ModuleException) ex).getProperty(); 97 } else { 98 error = new ActionMessage(ae.getKey(), ex.getMessage()); 99 property = error.getKey(); 100 } 101 102 this.logException(ex); 103 104 request.setAttribute(Globals.EXCEPTION_KEY, ex); 106 this.storeException(request, property, error, forward, ae.getScope()); 107 108 return forward; 109 110 } 111 112 113 118 protected void logException(Exception e){ 119 120 log.debug(messages.getMessage("exception.log"), e); 121 122 } 123 124 125 141 protected void storeException( 142 HttpServletRequest request, 143 String property, 144 ActionError error, 145 ActionForward forward, 146 String scope) { 147 148 this.storeException(request, property, (ActionMessage) error, forward, scope); 149 151 } 152 153 154 169 protected void storeException( 170 HttpServletRequest request, 171 String property, 172 ActionMessage error, 173 ActionForward forward, 174 String scope) { 175 176 ActionMessages errors = new ActionMessages(); 177 errors.add(property, error); 178 179 if ("request".equals(scope)) { 180 request.setAttribute(Globals.ERROR_KEY, errors); 181 } else { 182 request.getSession().setAttribute(Globals.ERROR_KEY, errors); 183 } 184 } 185 186 } 187 188 | Popular Tags |