蒋德的JAVA备忘录

JAVA备忘录

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  14 随笔 :: 4 文章 :: 5 评论 :: 0 Trackbacks

在用Struts 进行开发的时候,为了项目的并行,会有多个配置文件。
如 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();
    }

}


经过AutoActionServlet的转换,Strtus  就可以自动识别配置文件了

posted on 2008-03-20 09:11 蒋德 阅读(1439) 评论(2)  编辑  收藏

评论

# re: Struts 关于多个配置文件的自动识别 2008-03-24 13:39 xmp123
想法很好, 但是有时候并不是所有的struts config文件都在web-inf下.   回复  更多评论
  

# re: Struts 关于多个配置文件的自动识别 2009-03-22 16:04 石晓丹
com.allcom.base.core.module.PropertyManager
把这个类代码贴出来吧。
  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: