Xwork2 源码阅读(二)

Dispatcher类

Dispatcher类是在struts2中定义的,在 webwork2 中叫DispatcherUtils,

 

作用相同:

1 初始化模块参数,

2 将 xwork 于 web层分离,

3 中转请求,进入xwork 的 action处理流程。

 

1 初始化,涉及方法 init()

在上一篇的 FilterDispatcher中的init() 方法,有这么两句:

 

 

Java代码 复制代码
  1. dispatcher = createDispatcher(filterConfig);   
  2. dispatcher.init();  

 

就是调用Dispatcher的初始化。

 

2  xwork 定义了一个类 : ActonContext,

ActonContext 有两个作用

 

 1) 线程安全,

 过这个类,xwork2 实现了 action 的线程安全,

 为每一次的action请求, 分配一个独立的ActonContext。

 

 2) 于 web 层分离,

xwork的做法,是把web层中组件中的变量

(主要是javax.servlet包中的类,如HttpServletRequest)

取出来,放入ActonContext。

这样xwork就不要在向外访问web层了。

 

 

涉及方法

 

Java代码 复制代码
  1. public Map<String,Object> createContextMap(HttpServletRequest request, HttpServletResponse response,   
  2.         ActionMapping mapping, ServletContext context) {  

 

Dispatcher  的这个方法,把web层的参数取出然后放入Map 中,(以备xwork构造ActionContext)。

 

 

3 中转请求,调action处理逻辑,这个是比较重要的一个地方, 

Java代码 复制代码
  1. public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,   
  2.                           ActionMapping mapping) throws ServletException {   
  3.   
  4.     Map<String, Object> extraContext = createContextMap(request, response, mapping, context);   
  5.   
  6.     // If there was a previous value stack, then create a new copy and pass it in to be used by the new Action   
  7.     ValueStack stack = (ValueStack) request.getAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY);   
  8.     if (stack != null) {   
  9.         extraContext.put(ActionContext.VALUE_STACK, ValueStackFactory.getFactory().createValueStack(stack));   
  10.     }   
  11.   
  12.     String timerKey = "Handling request from Dispatcher";   
  13.     try {   
  14.         UtilTimerStack.push(timerKey);   
  15.         String namespace = mapping.getNamespace();   
  16.         String name = mapping.getName();   
  17.         String method = mapping.getMethod();   
  18.   
  19.         Configuration config = configurationManager.getConfiguration();   
  20.         ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(   
  21.                 namespace, name, extraContext, truefalse);   
  22.         proxy.setMethod(method);   
  23.         request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, proxy.getInvocation().getStack());   
  24.   
  25.         // if the ActionMapping says to go straight to a result, do it!   
  26.         if (mapping.getResult() != null) {   
  27.             Result result = mapping.getResult();   
  28.             result.execute(proxy.getInvocation());   
  29.         } else {   
  30.             proxy.execute();   
  31.         }   
  32.   
  33.         // If there was a previous value stack then set it back onto the request   
  34.         if (stack != null) {   
  35.             request.setAttribute(ServletActionContext.STRUTS_VALUESTACK_KEY, stack);   
  36.         }   
  37.     } catch (ConfigurationException e) {   
  38.         LOG.error("Could not find action or result", e);   
  39.         sendError(request, response, context, HttpServletResponse.SC_NOT_FOUND, e);   
  40.     } catch (Exception e) {   
  41.         throw new ServletException(e);   
  42.     } finally {   
  43.         UtilTimerStack.pop(timerKey);   
  44.     }   
  45. }  

 

 

            Configuration config = configurationManager.getConfiguration();
            ActionProxy proxy = config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(
                    namespace, name, extraContext, true, false);

得到框架的配置信息,得到框架的Container,构造出 ActionProxy。

这个过程中,涉及到几个xwork较核心的类,Configuration, ConfigurationManager, Container,

 

 

Configuration, ConfigurationManager 负责框架配置信息初始化的类

 

Java代码 复制代码
  1. public class DefaultConfiguration implements Configuration {   
  2.   
  3.     protected static final Logger LOG = LoggerFactory.getLogger(DefaultConfiguration.class);   
  4.   
  5.   
  6.     // Programmatic Action Configurations   
  7.     protected Map<String, PackageConfig> packageContexts = new LinkedHashMap<String, PackageConfig>();   
  8.     protected RuntimeConfiguration runtimeConfiguration;   
  9.     protected Container container;   
  10.     protected String defaultFrameworkBeanName;   
  11.     protected Set<String> loadedFileNames = new TreeSet<String>();   
  12.   
  13.   
  14.     ObjectFactory objectFactory;  

 

这是描述框架总体配置信息的,其中,

Map<String, PackageConfig> 放的是package的config,对应struts2 配置文件中 package标签内容,

 

PackageConfig

 

Java代码 复制代码
  1. public class PackageConfig extends Located implements Comparable, Serializable, InterceptorLocator {   
  2.   
  3.     private static final Logger LOG = LoggerFactory.getLogger(PackageConfig.class);   
  4.   
  5.     private Map<String, ActionConfig> actionConfigs;   
  6.     private Map<String, ResultConfig> globalResultConfigs;   
  7.     private Map<String, Object> interceptorConfigs;   
  8.     private Map<String, ResultTypeConfig> resultTypeConfigs;   
  9.     private List<ExceptionMappingConfig> globalExceptionMappingConfigs;   
  10.     private List<PackageConfig> parents;   
  11.     private String defaultInterceptorRef;   
  12.     private String defaultActionRef;   
  13.     private String defaultResultType;   
  14.     private String defaultClassRef;   
  15.     private String name;   
  16.     private String namespace = "";   
  17.     private boolean isAbstract = false;   
  18.     private boolean needsRefresh;  

 

里边是一些package的信息,其中Map<String, ActionConfig> 放的就是action的描述类ActionConfig了

 

ActionConfig

 


 

Java代码 复制代码
  1. public class ActionConfig extends Located implements Serializable {   
  2.   
  3.     public static final String WILDCARD = "*";   
  4.   
  5.     protected List<InterceptorMapping> interceptors;   
  6.     protected Map<String, String> params;   
  7.     protected Map<String, ResultConfig> results;   
  8.     protected List<ExceptionMappingConfig> exceptionMappings;   
  9.     protected String className;   
  10.     protected String methodName;   
  11.     protected String packageName;   
  12.     protected String name;   
  13.     protected Set<String> allowedMethods;  

 

 其中

interceptors : action的拦截器,

results: action执行后用来收尾的,如,跳到定义好的页面。
className 类名,packageName 包名,methodName 默认是 execute。

 

 

 

 

 

框架一般有个必须要做的活,就是把某配置文件的信息读到框架系统中来,

xwork2中,XMLConfigurationProvider 就是干这个的,

他读xml配置文件(就是我们定义action的配置的那个文件),

然后把这些信息写到Configuration中。

ConfigurationManager 则负责调用适当的 ConfigurationProvider,

执行初始化逻辑,和返回具体的Configuration。

 

初始化等前期工作完毕后,则开始执行action的逻辑了,

xwork 使用了代理模式执行action,

   proxy.execute();

 

从这一句起, strust2 就把工作交接给了xwork了。

你可能感兴趣的:(框架,Web,struts,servlet,Go)