蔣德的JAVA備忘錄

          JAVA備忘錄

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            14 隨筆 :: 4 文章 :: 5 評論 :: 0 Trackbacks

          在用Struts 進(jìn)行開發(fā)的時(shí)候,為了項(xiàng)目的并行,會有多個(gè)配置文件。
          如 struts-a.xml ; struts-b,xml ; struts-c.xml ;  
          這么多的配置文件如果都要手工配置到web.xml文件中,其實(shí)是很麻煩的。那么如何讓他自己動加載到struts-*.xml呢?
          如下:

          <servlet>
                  
          <servlet-name>action</servlet-name>
                  
          <servlet-class>com.allcom.base.util.AutoActionServlet</servlet-class>
                  
          <init-param>
                      
          <param-name>config</param-name>
                      
          <param-value>/WEB-INF/struts-*.xml</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>debug</param-name>
                      
          <param-value>3</param-value>
                  
          </init-param>
                  
          <init-param>
                      
          <param-name>detail</param-name>
                      
          <param-value>3</param-value>
                  
          </init-param>
                  
          <load-on-startup>5</load-on-startup>
              
          </servlet>



          在這里面,我用了通配符,所有由struts-開頭的.xml文件都可以進(jìn)行自動加載
          com.allcom.base.util.AutoActionServlet
          其源碼為:

          package com.allcom.base.util;

          import java.io.File;
          import java.util.ArrayList;
          import java.util.List;

          import javax.servlet.ServletException;

          import com.allcom.base.core.module.PropertyManager;
          /**
           * 
          @author <a href="mailto:jdskyy@163.com">Jdskyy</a>
           * 自動集成Struts 多個(gè)配置文件
           * 
           * 
          */

          public class AutoActionServlet extends org.apache.struts.action.ActionServlet {
              
          protected void initOther()throws ServletException {
                   String value 
          = getServletConfig().getInitParameter("config");
                   
          if(value != null && value.contains("*")){
                       List xmlfilelist 
          = new ArrayList();    
                       String path 
          = PropertyManager.getProperty("WebPath")+"WEB-INF";
                       File file 
          = new File(path);
                       
          if(!file.exists()) return;
                       
          if(!file.isDirectory()) return
                       String[] tempList 
          = file.list();
                       File temp 
          = null;
                       
          for(int i = 0; i < tempList.length; i++
                       
          {
                           
          if(path.endsWith(File.separator))
                               temp 
          = new File(path + tempList[i]);
                           
          else 
                               temp 
          = new File(path + File.separator + tempList[i]);
                              
                           
          if(temp.isFile()) {
                               String xmlFileName
          =temp.getName();
                               
          if(xmlFileName.startsWith("struts-")){
                                   System.out.println(xmlFileName);
                                   xmlfilelist.add(
          "/WEB-INF/"+xmlFileName);
                               }

                           }

                       }

                       String configStr 
          =ListTOString(xmlfilelist);
                       config 
          = configStr;
                  }

              }

              
              
          public static String ListTOString(List ids) {
                  StringBuffer listBufferStr 
          = new StringBuffer();
                  
          for(int i=0; i <ids.size() ;i++){
                      
          if(i==0){
                          listBufferStr.append(ids.get(i));
                      }
          else{
                            listBufferStr.append(
          ","+ids.get(i));
                      }

                  }

                  
          return listBufferStr.toString();
              }

          }


          經(jīng)過AutoActionServlet的轉(zhuǎn)換,Strtus  就可以自動識別配置文件了

          posted on 2008-03-20 09:11 蔣德 閱讀(1473) 評論(2)  編輯  收藏

          評論

          # re: Struts 關(guān)于多個(gè)配置文件的自動識別 2008-03-24 13:39 xmp123
          想法很好, 但是有時(shí)候并不是所有的struts config文件都在web-inf下.   回復(fù)  更多評論
            

          # re: Struts 關(guān)于多個(gè)配置文件的自動識別 2009-03-22 16:04 石曉丹
          com.allcom.base.core.module.PropertyManager
          把這個(gè)類代碼貼出來吧。
            回復(fù)  更多評論
            


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 新建县| 蕲春县| 文安县| 潮州市| 始兴县| 滨海县| 仪陇县| 大连市| 鄢陵县| 萨嘎县| 丽水市| 张家界市| 玉田县| 建湖县| 新蔡县| 商河县| 乐陵市| 无棣县| 阿城市| 松阳县| 井陉县| 东光县| 丁青县| 浦北县| 微山县| 枞阳县| 罗城| 清涧县| 石台县| 洪洞县| 神农架林区| 利辛县| 剑川县| 安庆市| 都兰县| 昌黎县| 黑山县| 磐安县| 利津县| 邛崃市| 安岳县|