Struts2学习-01

来源:互联网 发布:2016软件企业百强 编辑:程序博客网 时间:2024/06/10 23:28

1.Struts2执行过程

  • 首先httpservletrequest过来一个请求,这时由于在web.xml中设置了过滤器 strutsPrrpareAndExcuteFilter,请求会首先被这个过滤器进行过滤。
  • 然后根据配置文件struts.xml进行动作的映射(actionmapper),
  • 再一次的strutsPrrpareAndExcuteFilter过滤,然后执行动作类的代理类,依据struts.xml文件。然后去做动作调用。
  • actioninvocation,动作调用,所有的动作调用都是由ActionInvocation进行调用的。
  • 在动作调用时,由于框架里面有很多的默认拦截器,首先经过这些拦截器,然后执行我们自己写的动作,返回一个结果视图。
  • 根据结果视图,做相应的响应,如转发到指定的页面
  • 这些响应会经过框架的拦截器,比如数据的回显等。
  • 然后通过HttpservletResponse给出最终的响应与浏览器。

2.配置文件的加载顺序

配置文件的加载过程是由strutsPrrpareAndExcuteFilter的初始化过程的,StrutsPrepareAndExecuteFilter是一个过滤器,过滤器就有初始化方法,

InitOperations:

public DispatcherinitDispatcher( HostConfig filterConfig ) {

        Dispatcher dispatcher =createDispatcher(filterConfig);

        dispatcher.init();

        return dispatcher;

}

 

init_FileManager();

init_DefaultProperties(); // [1]

init_TraditionalXmlConfigurations();// [2]

init_LegacyStrutsProperties(); // [3]

init_CustomConfigurationProviders();// [5]

init_FilterInitParameters() ; // [6]

init_AliasStandardObjects(); // [7]

Dispatcher:分发器

 private void init_TraditionalXmlConfigurations() {

        String configPaths = initParams.get("config");

        if (configPaths ==null) {

            configPaths = DEFAULT_CONFIGURATION_PATHS;

        }

        String[] files = configPaths.split("\\s*[,]\\s*");

        for (String file : files) {

            if (file.endsWith(".xml")) {

                if ("xwork.xml".equals(file)) {

                    configurationManager.addContainerProvider(createXmlConfigurationProvider(file,false));

                } else {

                    configurationManager.addContainerProvider(createStrutsXmlConfigurationProvider(file,false,servletContext));

                }

            } else {

                throw new IllegalArgumentException("Invalid configurationfile name");

            }

        }

    }

通过以上代码可以得出配置文件的加载顺序:

一、default.properties:在struts2-core-**.jar包中的org.pache.struts2.default.properties中(框架本身用的)

二、struts-default.xml:在struts2-core-**.jar包中(框架本身用的)

三、struts-plugin.xml:在struts2-**-plugin.jar包中(插件中,给插件用的)

四、struts.xml:在你应用的构建路径中(程序员进行配置的地方。推荐)

五、struts.properties:在你应用的构建路径中(程序员进行配置的地方

六、web.xml:此处也可以对struts2框架进行配置(不建议。只建议配置过滤器)

3.动作的映射

  • ActionMapping mapping =prepare.findActionMapping(request, response,true);根据你的动作请求,从已经初始化好的配置文件中找对应的动作名称。

  • 分发:

    ActionProxy proxy =config.getContainer().getInstance(ActionProxyFactory.class).createActionProxy(

                        namespace, name, method,extraContext,true, false);



0 0
原创粉丝点击