蔣德的JAVA備忘錄

          JAVA備忘錄

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

          在用Struts 進行開發(fā)的時候,為了項目的并行,會有多個配置文件。
          如 struts-a.xml ; struts-b,xml ; struts-c.xml ;  
          這么多的配置文件如果都要手工配置到web.xml文件中,其實是很麻煩的。那么如何讓他自己動加載到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文件都可以進行自動加載
          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 多個配置文件
           * 
           * 
          */

          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)于多個配置文件的自動識別 2008-03-24 13:39 xmp123
          想法很好, 但是有時候并不是所有的struts config文件都在web-inf下.   回復  更多評論
            

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


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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 商丘市| 建德市| 乌拉特前旗| 襄汾县| 宝应县| 青铜峡市| 佛坪县| 定远县| 衡东县| 剑川县| 车致| 乌海市| 巴林右旗| 江源县| 延吉市| 辉县市| 靖西县| 安岳县| 双柏县| 惠来县| 漳州市| 文成县| 海兴县| 鄯善县| 巫山县| 松原市| 临潭县| 咸丰县| 渭源县| 射洪县| 手机| 绍兴县| 民丰县| 连云港市| 广河县| 新疆| 于都县| 贵南县| 崇文区| 德安县| 沾化县|