struts2工作流程分析

1.struts2架构图

2.几个概念

2.1 actionContext:  is the context in which an Action is executed. Each context is basically a container of objects an action needs for execution like the session, parameters, locale, etc.

    保存session,parameters等对象的是一个Map类型叫context对象。 其在内存中的快照如下:

 2.2 FilterDispatcher

 Master filter for Struts that handles four distinct responsibilities:

  • Executing actions
  • Cleaning up the ActionContext (see note)
  • Serving static content
  • Kicking off XWork's interceptor chain for the request lifecycle

Struts2.1.3之后用StrutsPrepareAndExecuteFilter 替换

2.3 Dsipatcher:

      struts真正用来转发的类。

     该类是线程安全的。因为它的实例是如下代码生成的   

private static ThreadLocal<Dispatcher> instance = new ThreadLocal<Dispatcher>();

public static Dispatcher getInstance() {
    return instance.get();
}
     用来处理的方法是serviceAction

public void serviceAction(HttpServletRequest request, HttpServletResponse response, ServletContext context,
                              ActionMapping mapping) throws ServletException
与Action相关的信息如name result path 在ActionMapping中。ActionMapping 的定义
public class ActionMapping {
    private String name;
    private String namespace;
    private String method;
    private String extension;
    private Map<String, Object> params;
    private Result result;

2.4 ActionInvocation:如何创建,执行Action。实现类DefaultActionInvocation。包含的对象有

protected Object action;
    protected ActionProxy proxy;
    protected List<PreResultListener> preResultListeners;
    protected Map<String, Object> extraContext;
    protected ActionContext invocationContext;
    protected Iterator<InterceptorMapping> interceptors;
    protected ValueStack stack;
    protected Result result;
    protected Result explicitResult;
    protected String resultCode;
    protected boolean executed = false;
    protected boolean pushAction = true;
    protected ObjectFactory objectFactory;
    protected ActionEventListener actionEventListener;
    protected ValueStackFactory valueStackFactory;
    protected Container container;
    private Configuration configuration;
    protected UnknownHandlerManager unknownHandlerManager;

     核心方法是invoke()

interceptor.getInterceptor().intercept(DefaultActionInvocation.this);//调用拦截器
invokeActionOnly();//调用Action

    其中invokeActionOnly(Object action, ActionConfig actionConfig)调用Action中对应的方法为:

Method method = null;
            try {
                method = getAction().getClass().getMethod(methodName, EMPTY_CLASS_ARRAY);
            }

if (!methodCalled) {
                methodResult = method.invoke(action, new Object[0]);//执行该方法
            }

    创建Action: createAction(Map<String, Object> contextMap)

action = objectFactory.buildAction(proxy.getActionName(), proxy.getNamespace(), proxy.getConfig(), contextMap);
//...
if (actionEventListener != null) {
            action = actionEventListener.prepare(action, stack);
        }

  

2.5 ActionProxy:怎么找到Action:存放actionName 和ActionInvocation对象。在ActionInvocation对象的初始化时创建Action:init(ActionProxy proxy)  中调用createAction(contextMap); struts中默认的实现类是StrutsActionProxy

 2.6 ObjectFactory   

ObjectFactory is responsible for building the core framework objects. Users may register their own implementation of the ObjectFactory to control instantiation of these Objects.

This default implementation uses the buildBean method to create all classes (interceptors, actions, results, etc).

     buildBean(config.getClassName(), extraContext)这个方法做的事情很简单:用ClassLoader加载classname所对应的class,extraContext这个参数没有用到。

2.7 ConfigurationManager - central for XWork Configuration management, including its ConfigurationProvider.

2.8 ConfigurationProvider interface describes the framework's configuration. By default, the framework loads its configurations via an xml document by using the StrutsXmlConfigurationProvider.

    

 

转载于:https://my.oschina.net/rainingcn/blog/14385

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值