⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 genericdispatcher.java

📁 webwork source
💻 JAVA
字号:
/* * WebWork, Web Application Framework * * Distributable under Apache license. * See terms of license at opensource.org */package webwork.dispatcher;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import webwork.action.factory.ActionFactory;import webwork.action.*;import webwork.config.Configuration;import webwork.util.BeanUtil;import webwork.util.ValueStack;import java.util.*;import java.beans.Beans;public class GenericDispatcher{   protected static Log log = LogFactory.getLog(GenericDispatcher.class);   private static ViewMapping mapping;   static   {      // Choose classloader      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();      // Initialize view mapping      String mappingName;      try      {         mappingName = Configuration.getString("webwork.viewmapping");      } catch (IllegalArgumentException e)      {         // Default         mappingName = DefaultViewMapping.class.getName();      }      try      {         mapping = (ViewMapping) Beans.instantiate(classLoader, mappingName);      } catch (Throwable t)      {         log.error("GenericDispatcher could not create ViewMapping object of class: " + mappingName, t);      }   }   boolean prepared = false;   Map oldContextTable;   ActionContext context;   ArrayList actions = new ArrayList();   String result;   String actionName;   LazyValueHolder lazyValueHolder;   boolean lazy = false;   Object view;   Exception actionException;   int initialStackSize;   public GenericDispatcher(String actionName)   {      this.actionName = actionName;   }   public GenericDispatcher(String actionName, boolean lazy)   {      this.actionName = actionName;      this.lazy = lazy;   }   /**   * Prepare a new ActionContext and return it      * prepareValueStack should be called after this when the new   * Valuestack has been set in the action context   */   public ActionContext prepareContext()   {      // Get oldContext and save      oldContextTable = ActionContext.getContext().getTable();      // Create new context      context = new ActionContext();      ActionContext.setContext(context);      context.put("action.chain", actions);      return context;   }      /**   * Prepare the dispatcher by saving the initial size    * of the current value stack   */   public void prepareValueStack()   {      initialStackSize = ActionContext.getValueStack().size();      prepared = true;   }   public ActionContext getOldContext()   {      ActionContext oldContext = new ActionContext();      oldContext.setTable(oldContextTable);      return oldContext;   }   public List getActions()   {      return actions;   }   public void executeAction() throws Exception   {      if (!prepared)      {         throw new Exception("You must prepare the dispatcher first");      }      Action action = ActionFactory.getAction(actionName);      if (lazy)      {         lazyValueHolder = new LazyValueHolder(action);         ActionContext.getValueStack().pushValue(lazyValueHolder);         // lazy evaluation can't chain         return;      } else      {         try         {            result = action.execute();         } catch (Exception e)         {            actionException = e;            return;         }         ActionContext.getValueStack().pushValue(action);      }      if (result != null)      {         try         {            view = mapping.getView(actionName, result);         } catch (Exception e)         {            view = null;         }      }      while (view instanceof ViewActionWrapper)      {         ViewActionWrapper viewActionWrapper = (ViewActionWrapper)view;         actionName = viewActionWrapper.getActionName();         action = ActionFactory.getAction(actionName);         if(viewActionWrapper.hasParams())            BeanUtil.setProperties(viewActionWrapper.getParams(), action);         try         {            result = action.execute();         } catch (Exception e)         {            actionException = e;            return;         }         ActionContext.getValueStack().pushValue(action);         if (result != null)         {            try            {               view = mapping.getView(actionName, result);            } catch (Exception e)            {               view = null;            }         }      }   }   public ActionResult finish()   {      if (lazy)      {         lazyValueHolder.getValue();         if (result != null)         {            try            {               view = mapping.getView(actionName, result);            } catch (Exception e)            {               view = null;            }         }      }      return new ActionResult(result, view, actions, actionException);   }   public void finalizeContext()   {      // Take all the actions off the stack of the old context      ValueStack vs = ActionContext.getValueStack();      int finalStackSize = vs.size();      int count = finalStackSize - initialStackSize;      for (int i = 0; i < count; i++)      {         vs.popValue();      }   	// Create a new context      ActionContext newContext = new ActionContext();      // Set old values in it      newContext.setTable(oldContextTable);      ActionContext.setContext(newContext);   }   public class LazyValueHolder implements ValueStack.ValueHolder   {      private Action action;      private boolean hasExecuted = false;      public LazyValueHolder(Action action)      {         this.action = action;      }      public Object getValue()      {         if (!hasExecuted)         {            hasExecuted = true;            try            {               result = action.execute();            } catch (Exception e)            {               actionException = e;               result = null;               return null;            }         }         return action;      }   }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -