How to automatically load an XML file in Struts2 (without modifying the configuration file)

Source: Internet
Author: User
1. Rewrite the three methods of the Filterdispatcher class, the path of my struts-*.xml under the Web-inf/modules/struts folder

, the Jltenvironment class is the configuration path for my app Java code public class jltfilterdispatcher extends filterdispatcher  {     @Override     protected dispatcher createdispatcher ( Filterconfig rconfig)          {         map <string, string> params = new hashmap <string,  String> ();          for  (enumeration e =   Filterconfig.getinitparameternames);              e .hasmoreelements ();)        {             string name  =  (String)  e.nextelement ();              String value = filterconfig.getinitparameter (name);             params.put (name, value);       }       //  load struts profiles under modules        getstrutsconfig (params);         return      new dispatcher (filterconfig.getservletcontext (),  params);    }      //  load struts configuration file under modules      Private void getstrutsconfig (map <string, string> m)  {        string strutspath =       new string (  " Struts-default.xml,struts-plugin.xml,struts.xml ");        File f =  New file (Jltenvironment.getmoduleshome () + "/struts");        if  ( F.getname (). Equals ("struts"))  {               File[] fF = f.listfiles (); if  (ff != null && ff.length >  0)  {              for  (int i =  0; i < ff.length; i++)  {                     string fname = ff[i].getname ();                     if   (Fname.startswith ("struts-")                          && fname.endswith (". xml"))  {                          strutspath+= "," +ff[i].getabsolutepath ();                    }              }         }         m.put ("config",  strutspath);   }       @Override      public void init (filterconfig  Filterconfig)  throws ServletException {        //Get the path to the app        servletcontext ctx = filterconfig.getservletcontext ();         string home = ctx.getrealpath ("/");       home = home.replace (' \ \ ',  '/');         if  (!home.endswith ("/"))  {             home = home +  "/";       }      //Initialization of application environment parameters      &NBSP;&NBSp Jltenvironment.init (HOME,&NBSP;CTX);  super.init (filterconfig);    }   

public class Jltfilterdispatcher extends Filterdispatcher {@Override protected Dispatcher createdispatcher (filterconf       IG Rconfig) {Map <string, string> params = new HashMap <string, string> ();            For (enumeration e = Filterconfig.getinitparameternames);     E. hasmoreelements ();)           {String name = (string) e.nextelement ();          String value = filterconfig.getinitparameter (name);     Params.put (name, value);      }//Load Struts configuration file Getstrutsconfig (params) under modules;   return new Dispatcher (Filterconfig.getservletcontext (), params);     }//Load struts configuration file under modules private void Getstrutsconfig (Map <string, string> m) {String Strutspath =      New String ("Struts-default.xml,struts-plugin.xml,struts.xml");      File F = new file (jltenvironment.getmoduleshome () + "/struts");         if (F.getname () equals ("struts")) {file[] ff = F.listfiles (); if (ff! = null && ff.length > 0) {   for (int i = 0; i < ff.length; i++) {String fname = Ff[i].getname (); if (Fname.startswith ("struts-") && Fname.endswith (". xml")) {Strutspath                  + = "," +ff[i].getabsolutepath ();  }}} m.put ("config", Strutspath); } @Override public void init (Filterconfig filterconfig) throws Servletexception {//Get the applied path ServletContext C      tx = Filterconfig.getservletcontext ();     String home = Ctx.getrealpath ("/");      Home = home.replace (' \ \ ', '/');     if (!home.endswith ("/")) {home = home + "/"; }//Initialize the application environment parameter Jltenvironment.init (home, CTX);   Super.init (Filterconfig);  }

2.web.xml Change to

XML code <filter> <filter-name>struts2 </filter-name> <filter-class> com.jlt.c Ore. Jltfilterdispatcher </filter-class> </filter>

<filter>      <filter-name>struts2 </filter-name>     <filter-class>           Com.jlt.core.JLTFilterDispatcher      </filter-class>  </filter>

3. In this way, all XML files ending with struts-beginning with web-inf/modules/struts will be loaded automatically,

No need to change the other configuration, hehe

If you add the struts profile again, it will be loaded automatically as long as it is placed in the Web-inf/modules/struts directory.

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.