`

实现xwork配置文件的自动加载-摘自http://blog.csdn.net/snow_fox_yaya/article/details/2218141

 
阅读更多
一、
如果配置文件struts.xml的内容太多,或者是团队开发时希望各自struts2 的Action配置独立,而不互相干扰,可以将配置文件拆分为多个文件,这样可以减少工作量,便于维护。

常见的做法在默认的struts.xml中使用include来包含各个模块的配置文件。如果是多个配置文件时,就使用多个include元素。新加模块的action配置时,还是要修改struts.xml。
于是,有人提出来,是不是可以在不修改文件时,自动引入各个模块的配置文件呢? 当然,我们可以重写Filter类,配合使用Filter初始化参数来实现多配置文件的自动引入。 事实上,还有更简单的方法,那就是在include中使用通配符:
Xml代码 
<struts>    
       ... ...    
    <include file="config/struts-*.xml"></include>    
</struts>   

然后,将各个模块的配置文件放在config包下就可以了。




二、本公司的业务开发平台中,在展现层用到了webwork作为控制框架,由于最近在公司的一个比较大的商业计划中,打算实现“应用集市”的功能,在技术上要实现业务模块的导入导出功能,因此要求系统必须能够实现自动加载一个业务模块的所有相关文件,而在持久层和业务层不存在任何问题,因为hibernate的mapping文件,和spring的配置文件xxx.bean.xml只要在classpath中,都是可以自动加载的,但是对于webwork的配置文件,用过的人都知道,一个单独的模块配置文件:xxxmodule_xwork.xml必须要在系统的xwork.xml文件中显式地include进来,这个模块的配置文件才能够自动地被加载到,实在是烦人。因此笔者想到了,能不能也像spring的配置文件一样,能够放到classpath下被自动地加载到,于是偶赶紧去看xwork的源码,终于被偶发现:xwork的配置文件加载都是依赖于com.opensymphony.xwork.config.providers.XmlConfigurationProvider这个类, 其中init方法的源码如下:

public void init(Configuration configuration) {
        this.configuration = configuration;
        includedFileNames.clear();


        try {
            loadConfigurationFile(configFileName, null);
        } catch (ConfigurationException e) {
            throw e;
        } catch (Exception e) {
            LOG.fatal("Could not load XWork configuration file, failing", e);
            throw new ConfigurationException("Error loading configuration file " + configFileName, e);
        }
    }

可以看到loadConfigurationFile()这个方法真正地实现xwork.xml文件的加载,如果我在系统启动的时候先取到classpath中所有的xxxmodule_xwork.xml文件,然后传给这个init方法,循环调用loadConfigurationFile()这个方法,不就可以把classpath中所有的xwork相关的配置文件自动加载了么,因此代码改写如下:

    //add by xinpeng 20080316
    private List configFileNameList;

    //加入一个构造方法,将classpath下所有的xwork文件名称的集合传入
    public XmlConfigurationProvider(List configFileNameList) {
        this.configFileNameList = configFileNameList;
    }
    //end by xinpeng

public void init(Configuration configuration) {
        this.configuration = configuration;
        includedFileNames.clear();
        try {
            //modify by xinpeng 20080314,循环加载所有的xwork配置文件
            for (int i = 0, len = configFileNameList.size(); i < len; i++) {
                String configFileName = (String) configFileNameList.get(i);
                this.configFileName = configFileName;
                LOG.debug("the current config file name is : "+this.configFileName);
                loadConfigurationFile(configFileName, null);
            }
            //end by xinpeng
        } catch (Exception e) {
            LOG.fatal("Could not load XWork configuration file, failing:" + e);
            throw new ConfigurationException("Error loading configuration file " + configFileNameList, e);
        }
    }

然后再写一个监听器,在系统启动的时候,取得classpath下所有的xwork的配置文件,并通过XmlConfigurationProvider类的构造子传入,监听器的监听方法如下:

public void contextInitialized(ServletContextEvent event) {
        log.info("beginning to load xwork config file......");

        List<String> xworkConfigFileList = new ArrayList<String>();
        String[] fileList = ResourceFinder.getXwork();

        for (int i = 0, len = fileList.length; i < len; i++) {
            xworkConfigFileList.add(fileList[i]);
        }
        ConfigurationProvider configurationProvider = new XmlConfigurationProvider(xworkConfigFileList);
        ConfigurationManager.addConfigurationProvider(configurationProvider);
        log.info("all the xwork config file has been loaded successfully!");
    }

最后在web.xml文件中,配置上这个监听器,把xwork.xml文件中n多的include全部干掉,重启tomcat,OK全部成功了!,以后再也不用关心xwork配置文件的include问题,我们可以直接把它打包到xxxmodule.jar中,从而真正地实现了xwork配置文件自动加载功能。
分享到:
评论

相关推荐

    struts-2.1.8.1-src.zip

    EZMorph (http://ezmorph.sourceforge.net/) FreeMarker (http://freemarker.org/). JSON-lib (http://json-lib.sourceforge.net/). OGNL (http://www.opensymphony.com/ognl/). Plexus ...

    struts2-core-2.3.15.3.jar

    EZMorph (http://ezmorph.sourceforge.net/) FreeMarker (http://freemarker.org/). JSON-lib (http://json-lib.sourceforge.net/). OGNL (http://www.opensymphony.com/ognl/). Plexus ...

    struts-2.3.15.3-apps.zip

    EZMorph (http://ezmorph.sourceforge.net/) FreeMarker (http://freemarker.org/). JSON-lib (http://json-lib.sourceforge.net/). OGNL (http://www.opensymphony.com/ognl/). Plexus ...

    struts-2.5.13-docs

    EZMorph (http://ezmorph.sourceforge.net/) FreeMarker (http://freemarker.org/). JSON-lib (http://json-lib.sourceforge.net/). OGNL (http://www.opensymphony.com/ognl/). Plexus ...

    xwork-2.0.4-src

    &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;project xmlns="... xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;

    struts2入门实例4 经典入门必备

    字段校验和非字段校验----http://blog.csdn.net/loverszhaokai/archive/2010/12/07/6059686.aspx ----http://blog.csdn.net/loverszhaokai/archive/2010/12/03/6052056.aspx 12.Struts2_04_validate_properties ...

    struts2入门实例3 经典入门必备

    字段校验和非字段校验----http://blog.csdn.net/loverszhaokai/archive/2010/12/07/6059686.aspx ----http://blog.csdn.net/loverszhaokai/archive/2010/12/03/6052056.aspx 12.Struts2_04_validate_properties ...

    struts2入门实例2 经典入门必备

    字段校验和非字段校验----http://blog.csdn.net/loverszhaokai/archive/2010/12/07/6059686.aspx ----http://blog.csdn.net/loverszhaokai/archive/2010/12/03/6052056.aspx 12.Struts2_04_validate_properties ...

    struts2入门实例1

    字段校验和非字段校验----http://blog.csdn.net/loverszhaokai/archive/2010/12/07/6059686.aspx ----http://blog.csdn.net/loverszhaokai/archive/2010/12/03/6052056.aspx 12.Struts2_04_validate_properties ...

    ssh+mysql55jar包集合

    三大框架的jar包,能够跑得起来项目. /xscjManager/WebContent/WEB-INF/lib/antlr.jar /xscjManager/WebContent/WEB-INF/lib/asm.jar .../xscjManager/WebContent/WEB-INF/lib/xwork-core-2.3.4.1.jar

    xwork各版本的jar包

    xwork-1.1.1.jar, xwork-1.1.jar, xwork-1.2.1.jar, xwork-1.2.3.jar, xwork-2.0.0.jar, xwork-2.0.1.jar, xwork-2.0.3.jar, xwork-2.0.4.jar, xwork-2.0.5.jar, xwork-2.0.6.jar, xwork-2.0.7.jar, xwork-2.1.0.jar...

    struts2中json依赖包

    logging-1.1.1.jar/ezmorph-1.0.6.jar/freemarker-2.3.22.jar/javassist-3.11.0.GA.jar/json-lib-2.4-jdk15.jar/log4j-api-2.2.jar/log4j-core-2.2.jar/ognl-3.0.6.jar/struts2-core-2.3.24.jar/xwork-core-2.3.24....

    Struts 2 远程代码执行漏洞(s2-045\s2-046)修复所用到的包

    1.严格过滤 Content-Type 、filename里的内容,严禁ognl表达式相关字段。 2.如果您使用基于...xwork-core-2.3.32.jar --来源 http://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin/2.3.32

    struts2.1.6+spring2.0+hibernate3.2常用配置包

    例子:xwork-2.1.2.jar在2.1.8中xwork-core-2.1.6.jar 具体啥问题没测过 3 hibernate3相关包 antlr-2.7.2.jar //2 aopalliance-1.0.jar //2 asm-attrs.jar //3 asm.jar // 3 cglib-2.1.3.jar //3 commons-...

    xwork-core-2.3.15.1.jar

    xwork-core-2.3.15.1.jar

    Jquery拖拽Div层排序8中拖拽效果

    直接8中拖拽效果,其中可以随意鼠标位置拖动,可以按照列进行位置拖动,只需要吧jar集成到项目中就可以直接使用

    struts2.3.20版本的jar包

    这个是struts2.3.20版本的所需要的九个包 commons-fileupload-1.3.1.jar commons-io-2.2.jar commons-lang3-3.2.jar freemarker-2.3.19.jar javassist-3.11.0.GA.jar ognl-...plugin-2.3.20.jar xwork-core-2.3.20.jar

    xwork--- jar包

    一下儿关于xwork--- jar包,开发的时候有用

    openlogic-xwork-2.0.7-all-src-1.zip

    openlogic-xwork-2.0.7-all-src-1.zip

Global site tag (gtag.js) - Google Analytics