Implementation of STRUTS2 configuration file full path search

Source: Internet
Author: User
Tags inheritance
When we use STRUTS2, the associated action profile (typically struts.xml as a configuration file), Struts2 strutsprepareandexecutefilter filter to the engineering Classpath (web-inf/ Classes directory) to find this profile.
In this way, there are a lot of XML configuration files under the Classpath, which is inconvenient for maintenance, and we typically place our custom struts.xml in a special directory, such as in the Web-inf/conf directory. At this point, our struts filter in Web.xml is configured like this:
    	<!--definition Struts2 core filter-->
	<filter>
		<filter-name>struts2</filter-name>
		< filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
		</ filter-class>
		<init-param>  
        	<param-name>config</param-name>  
        	<param-value >struts-default.xml,struts-plugin.xml,.. /conf/struts.xml</param-value>  
    	</init-param>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</ Filter-mapping>

After that, if you want to add more struts configurations, you can configure config file values in this way, or you can configure additional profiles with the include tag in Struts.xml:

    <struts>
        <include file= ". /conf/other-struts.xml "/><!--Note: Here is also the relative classpath configuration--> ...
    </struts>

As you can see, this configuration is either struts.xml, flooded with action configurations, or flooded with many include tags. and also need to focus on a variety of path issues (especially, if we need to follow the action class package path relationship, in the Config directory also according to the package path to create a folder, the corresponding struts configuration file under the corresponding package, the path problem will be dizzy).

In this regard, we can consider whether the filter can initialize the configuration file processing mechanism, to implement the need for additional configuration, so that the filter automatically "search" the Struts configuration file.

Let's first explain how a configuration file is placed. Under each action class package, create a conf directory that specializes in Struts.xml configuration files. In this way, our action class and configuration files are managed together, very flexible and convenient. The next step is how to implement the full path search for the struts configuration file.

The first step is to analyze how the Strutsprepareandexecutefilter filter implements the configuration file lookup. Through analysis, in the Org/apache/struts2/dispatcher/ng/initoperations.java class Createdispatcher method, has done the path lookup work. Here you can look at the implementation of the Createdispatcher method, the specific call relations refer to the relevant source code:

    /**
     * Create a {@link Dispatcher}
    /Private Dispatcher Createdispatcher (Hostconfig filterconfig) {
        map<string, string> params = new hashmap<string, string> ();
        for (iterator E = Filterconfig.getinitparameternames (); E.hasnext ();) {
            String name = (string) e.next ();
            String value = filterconfig.getinitparameter (name);
            Params.put (name, value);
        }
        return new Dispatcher (Filterconfig.getservletcontext (), params);
    }

This will enclose the struts configuration file as "," in a string, encapsulated in the params map.

Here, we consider doing a full path search before the return operation, assembling the entire path of the other Struts.xml configuration file into the profile string. Modifying the Initoperations.java class directly is not conducive to Lib library management, so consider using inheritance to rewrite the Strutsprepareandexecutefilter filter init method, and the Initoperations.java class Createdi Spatcher method.

The class that inherits Initoperations (may be named Myinitoperations) overrides the private Dispatcher Createdispatcher (Hostconfig filterconfig) method, The reference implementation is as follows:

    /** * Cover Original method * * Private Dispatcher Createdispatcher (Hostconfig hostconfig) {map<string, S
        tring> params = new hashmap<string, string> ();
        String name;
	String value;
            The original logic is reserved for (iterator<? > E = Hostconfig.getinitparameternames (); E.hasnext (); Params.put (name,
            Value)) {name = (String) e.next ();
        Value = Hostconfig.getinitparameter (name);
        } parsestrutsconfig (Hostconfig, params);

    return new Dispatcher (Hostconfig.getservletcontext (), params); /** * Gets all configuration file paths/private void Parsestrutsconfig (Hostconfig hostconfig, map<string, string>
        m) {String home = Hostconfig.getservletcontext (). Getrealpath ("/");
        Home.replace ("//", "/");
        if (Home.startswith ("/")) {home = home + "/"; }//Initialize StringBuilder Strutspath = new StringBuilder ("Struts-default.xml,struts-plugIn.xml ");
        Getstrutspath (New File (home), Strutspath);
    M.put ("config", strutspath.tostring ());
        /** * Get all Struts configuration files under Project */private void Getstrutspath (file file, StringBuilder Pathbuilder) {
        File is empty, do not manipulate if (null = = file) {return;
        } file[] files = file.listfiles ();
	    for (int i = 0; i < files.length i++) {File f = files[i];
                If the directory file, recursively call this method if (F.isdirectory ()) {Getstrutspath (f, Pathbuilder);
            Continue
            //Resolve filename: At end of Struts.xml, string fName = Files[i].getname () is spliced into the path string;
		if (Fname.endswith ("Struts.xml")) {Pathbuilder.append (",");
            Absolute path Pathbuilder.append (Files[i].getabsolutepath ());
 }
        }
    }


The Strutsprepareandexecutefilter class Init method source code is as follows:

    public void init (Filterconfig filterconfig) throws servletexception {
        initoperations init = new Initoperations (); 
  dispatcher Dispatcher = null;
        try {
            Filterhostconfig config = new Filterhostconfig (filterconfig);
            init.initlogging (config);
            Dispatcher = init.initdispatcher (config);
            Init.initstaticcontentloader (config, dispatcher);

            Prepare = new Prepareoperations (Filterconfig.getservletcontext (), dispatcher);
            Execute = new Executeoperations (Filterconfig.getservletcontext (), dispatcher);
            This.excludedpatterns = init.buildexcludedpatternslist (dispatcher);

            Postinit (Dispatcher, filterconfig);
        } Finally {
            if (dispatcher!= null) {
                dispatcher.cleanupafterinit ();
            }
            Init.cleanup ();
        }
    


    inheriting class (assumed to be mystrutsprepareandexecutefilter) overrides this method, initoperations init = new Initoperations (); The operation is changed to myinitoperations init = new Myinitoperations ();
    Here are a few instructions to use:
    1. Web.xml's struts core filter needs to be configured to mystrutsprepareandexecutefilter this inheriting class;
    2. Struts configuration file must be "Struts.xml" Ends to differentiate between other profiles;
    3. As long as the absolute path is different, the configuration file uses the same file name (for example, all with "Struts.xml" as the profile name) without effect;
     4. The configuration file can be resolved by the class loading path, which is its subpath;
    5. If you need to resolve the non class loading path directory, you need to refer to this way, according to the relative path idea, get a new directory file, call Getstrutspath ( File file, StringBuilder Pathbuilder) method.

    is implemented using the idea of inheritance and full path traversal, which seems relatively straightforward. So, under our class loading path, the full path search struts configuration file is implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.