0,0 → 1,62 |
package ak.webcontrol.core.action; |
|
import javax.servlet.ServletException; |
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
|
import org.apache.struts.Globals; |
import org.apache.struts.action.ActionError; |
import org.apache.struts.action.ActionErrors; |
import org.apache.struts.action.ActionMessages; |
import org.apache.struts.action.ExceptionHandler; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.action.ActionForm; |
import org.apache.struts.action.ActionForward; |
import org.apache.struts.config.ExceptionConfig; |
|
import org.apache.log4j.Logger; |
|
import ak.strutsx.ErrorHandlerX; |
|
import ak.webcontrol.util.UserException; |
import ak.webcontrol.util.FormException; |
|
public final class UserExceptionHandler |
extends ExceptionHandler |
{ |
private static final Logger logger = Logger.getLogger(UserExceptionHandler.class); |
|
public ActionForward execute(Exception ex, ExceptionConfig config, |
ActionMapping mapping, ActionForm formInstance, |
HttpServletRequest request, HttpServletResponse response) |
throws ServletException |
{ |
if(!(ex instanceof UserException)) |
throw new ServletException("Unknown type of exception: " + ex.getClass()); |
|
UserException userEx = (UserException)ex; |
logger.info("begin exception handle:" + userEx.getMessage()); |
|
// try to get property for this exception if any |
String property = ActionMessages.GLOBAL_MESSAGE; |
if(userEx instanceof FormException) { |
FormException formEx = (FormException)userEx; |
if(formEx.getProperty() != null) |
property = formEx.getProperty(); |
} |
|
// create new error message |
ActionErrors errors = (ActionErrors)request.getAttribute(Globals.ERROR_KEY); |
if(errors == null) { |
errors = new ActionErrors(); |
request.setAttribute(Globals.ERROR_KEY, errors); |
} |
errors.add(property, new ActionError(userEx.getMessage(), userEx.getValues())); |
|
// find forward |
if(mapping.getInput() == null) |
return mapping.findForward("error"); |
else |
return mapping.getInputForward(); |
} |
} |