隨筆-6  評論-0  文章-3  trackbacks-0

          package com.gxlu.common.web.webwork.intercepter;

          import java.io.StringWriter;
          import java.util.HashMap;
          import java.util.Iterator;
          import java.util.Map;
          import java.util.Properties;

          import org.apache.commons.logging.Log;
          import org.apache.commons.logging.LogFactory;

          import com.gxlu.common.web.webwork.action.AjaxJSONAction;
          import com.gxlu.common.web.webwork.action.AjaxUpdaterAction;
          import com.gxlu.common.web.webwork.action.AjaxXMLAction;
          import com.opensymphony.webwork.ServletActionContext;
          import com.opensymphony.xwork.Action;
          import com.opensymphony.xwork.ActionInvocation;
          import com.opensymphony.xwork.interceptor.Interceptor;

          /**
          ?* Interceptor to handle Exceptions thrown by Actions.
          ?* <p>
          ?* <a href="ExceptionHandlerInterceptor.java.html"> <i>View Source </i> </a>
          ?* </p>
          ?*?
          ?*/
          public class ExceptionInterceptor implements Interceptor {
          ?public static final String EXCEPTION = "exception";

          ?protected final Log logger = LogFactory.getLog(getClass());

          ?private Map exceptionMappings;

          ?/**
          ? * Set the mappings between exception class names and result names.
          ? *
          ? * @param mappings
          ? *??????? fully qualified exception class names as keys, and result names as
          ? *??????? values
          ? */
          ?public void setExceptionMappings(Properties mappings) throws ClassNotFoundException {
          ??this.exceptionMappings = new HashMap();

          ??for (Iterator it = mappings.keySet().iterator(); it.hasNext();) {
          ???String exceptionClassName = (String) it.next();
          ???String viewName = mappings.getProperty(exceptionClassName);
          ???Class exceptionClass = Class.forName(exceptionClassName, true, Thread.currentThread().getContextClassLoader());
          ???this.exceptionMappings.put(exceptionClass, viewName);
          ??}
          ?}

          ?/**
          ? * Invoke action and if an exception occurs, route it to the mapped result.
          ? */
          ?public String intercept(ActionInvocation invocation) throws Exception {
          ??String result = null;

          ??try {
          ???result = invocation.invoke();
          ??} catch (Throwable e) {
          ???logger.error(e);
          ???//
          ???result = EXCEPTION;
          ???//
          ???StringWriter writer = new StringWriter();
          ???e.printStackTrace(new java.io.PrintWriter(writer));
          ???ServletActionContext.getRequest().setAttribute("_exception_stack_trace_", writer.toString());
          ???ServletActionContext.getRequest().setAttribute("_exception_message_", e.getMessage());
          ???writer = null;
          ???Action action = invocation.getAction();
          ???if(action instanceof AjaxJSONAction) {????
          ????return "exception.json";
          ???} else if(action instanceof AjaxXMLAction) {
          ????return "exception.xml";
          ???} else if(action instanceof AjaxUpdaterAction) {
          ????return "exception.updater";
          ???}
          ???/**
          ??? * // check for specific mappings if (this.exceptionMappings !=
          ??? * null) { for (Iterator it =
          ??? * this.exceptionMappings.keySet().iterator(); it.hasNext();) {
          ??? * Class exceptionClass = (Class) it.next(); if
          ??? * (exceptionClass.isInstance(ex)) { result = (String)
          ??? * this.exceptionMappings.get(exceptionClass); } } } if (null ==
          ??? * result || "".equals(result)) { result = EXCEPTION; }
          ??? */
          ??}
          ??return result;

          ?}

          ?public void destroy() {
          ?}

          ?public void init() {
          ?}
          }

          posted on 2006-11-22 22:57 frogfool 閱讀(352) 評論(0)  編輯  收藏 所屬分類: WebWork

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 芦山县| 赣榆县| 临高县| 黄龙县| 隆子县| 高安市| 南江县| 锡林郭勒盟| 浮山县| 闽侯县| 拉萨市| 巴楚县| 乡宁县| 慈溪市| 浮梁县| 龙江县| 阿巴嘎旗| 宜昌市| 仙居县| 浦江县| 巨鹿县| 正镶白旗| 镇沅| 大方县| 广丰县| 集贤县| 仁怀市| 嫩江县| 施秉县| 哈尔滨市| 运城市| 河源市| 香港 | 济南市| 高陵县| 襄城县| 拜城县| 阿荣旗| 铅山县| 遂平县| 广元市|