递归读取并解释多配置文件

系统 1486 0


递归读取并解释多配置文件
 

 最近做项目时,遇到了多配置文件读取的问题。最后,还是采用了递归读取配置文件的方法去实现,感觉挺实用的。

 

    package com.lxit.web.util;

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

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

import com.lxit.web.util.bean.ActionFormTemplate;
import com.lxit.web.util.bean.ActionTemplate;
import com.lxit.web.util.bean.DispatchTemplate;
import com.lxit.web.util.bean.ServletHelper;



public class LoadConfigServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private final static String CONFIG_PATH="\\WEB-INF\\config";
    private List<String> fileList= new ArrayList<String>();
    private List<ServletHelper> ServletHelperList= new ArrayList<ServletHelper>();
    private Document  document;

	@Override
    public void init(ServletConfig config) throws ServletException {
		getConfigFiles(config.getServletContext(),CONFIG_PATH);
		for(String file:fileList){
			System.out.println("读取配置文件:"+file);
			ServletHelperList.add(parseConfigFile(file));
		}
		System.out.println("ServletHelperList的值如下:");
		for(ServletHelper i: ServletHelperList){
           System.out.println("i:   "+i);
		}
	}

	public void getConfigFiles(ServletContext context,String path){
		String filePath=context.getRealPath("")+path;
		File file=new File(filePath);
		File[] files=file.listFiles();
		for(File i:files){
			if(i.isDirectory()){
			   getConfigFiles(context,CONFIG_PATH+"\\"+i.getName());
			}else if(i.isFile()){
			    fileList.add(filePath+"\\"+i.getName());
			}
		}
	}

	private ServletHelper parseConfigFile(String path){
        ServletHelper servletHelper=new ServletHelper();
        SAXBuilder saxBuilder = new SAXBuilder();
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(path);
            document=saxBuilder.build(fileInputStream);
        } catch (Exception e) {
            System.out.println("配置文件加载失败    路径:  "+path);
            e.printStackTrace();
        }
        Element rootElement = document.getRootElement();
        List<Element> childlist = rootElement.getChildren();
        for (Element tempElement : childlist) {
            if(tempElement.getName().equals("actionForm")){
               ActionFormTemplate actionFormTemplate=new ActionFormTemplate(tempElement.getAttributeValue("name"),tempElement.getAttributeValue("class"));
               servletHelper.setActionFormTemplate(actionFormTemplate);
            }else if(tempElement.getName().equals("action")){
               ActionTemplate actionTemplate=new ActionTemplate(tempElement.getAttributeValue("name"),tempElement.getAttributeValue("class"));
               List<Element> actionChildElement=tempElement.getChildren();
               for(Element actionElement:actionChildElement){
                   DispatchTemplate dispatchTemplate=new DispatchTemplate(actionElement.getAttributeValue("name"),actionElement.getText());
                   actionTemplate.setDispatchTemplate(dispatchTemplate);
               }
               servletHelper.setActionTemplate(actionTemplate);
            }
        }
        return servletHelper;
	}

	@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}


	@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

}

  

 

    package com.lxit.web.util.bean;

public class ActionFormTemplate {
    private String name;
    private String Class;

    public ActionFormTemplate(String name,String Class){
        this.name=name;
        this.Class=Class;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getActionFormClass() {
        return Class;
    }
    public void setActionFormClass(String Class) {
        this.Class = Class;
    }
    @Override
    public String toString() {
        return "ActionFormTemplate [name=" + name + ", Class=" + Class + "]";
    }


}

  

 

    package com.lxit.web.util.bean;

import java.util.HashMap;
import java.util.Map;

public class ActionTemplate {
    private String name;
    private String Class;
    private Map<String,DispatchTemplate> dispatchTemplate=new HashMap<String,DispatchTemplate>();

    public ActionTemplate(String name,String Class){
        this.name=name;
        this.Class=Class;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getActionClass() {
        return Class;
    }
    public void setActionClass(String Class) {
        this.Class = Class;
    }
    public Map<String, DispatchTemplate> getDispatchTemplate() {
        return dispatchTemplate;
    }
    public void setDispatchTemplate(DispatchTemplate tempDispatchTemplate) {
        dispatchTemplate.put(tempDispatchTemplate.getName(),tempDispatchTemplate);
    }
    @Override
    public String toString() {
        return "ActionTemplate [name=" + name + ", Class=" + Class + ", dispatchTemplate=" + dispatchTemplate + "]";
    }


}

  

 

    package com.lxit.web.util.bean;


public class DispatchTemplate {
    private String name;
    private String text;

    public DispatchTemplate(String name,String Text){
        this.name=name;
        this.text=Text;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    @Override
    public String toString() {
        return "DispatchTemplate [name=" + name + ", text=" + text + "]";
    }


}

  

 

    package com.lxit.web.util.bean;

import java.util.HashMap;
import java.util.Map;

public class ServletHelper {
    private Map<String,ActionFormTemplate> actionFormTemplate=new HashMap<String,ActionFormTemplate>();
    private Map<String,ActionTemplate> actionTemplate=new HashMap<String,ActionTemplate>();


    public Map<String, ActionFormTemplate> getActionFormTemplate() {
        return actionFormTemplate;
    }
    public void setActionFormTemplate(ActionFormTemplate tempActionFormTemplate) {
        actionFormTemplate.put(tempActionFormTemplate.getName(),tempActionFormTemplate);
    }
    public Map<String, ActionTemplate> getActionTemplate() {
        return actionTemplate;
    }
    public void setActionTemplate(ActionTemplate tempActionTemplate) {
        actionTemplate.put(tempActionTemplate.getName(),tempActionTemplate);
    }
    @Override
    public String toString() {
        return "ServletHelper [actionFormTemplate=" + actionFormTemplate + ", actionTemplate=" + actionTemplate + "]";
    }


}

  

 user.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<config>
<!-- actionForm反射地址  -->
	<actionForm name="AccountForm" class="com.lxit.book.bean.Account"></actionForm>
	<actionForm name="BookForm" class="com.lxit.book.bean.Book"></actionForm>
	<actionForm name="BorrowBookForm" class="com.lxit.book.bean.BorrowBook"></actionForm>
	<actionForm name="StudentForm" class="com.lxit.book.bean.Student"></actionForm>
	<actionForm name="UserForm" class="com.lxit.book.bean.User"></actionForm>
	<actionForm name="DictionaryForm" class="com.lxit.book.bean.DictionaryValue"></actionForm>
	<actionForm name="RoleForm" class="com.lxit.book.bean.Role"></actionForm>
	<actionForm name="PurviewForm" class="com.lxit.book.bean.Purview"></actionForm>
<!-- method 反射地址 -->
    <action name="book" class="com.lxit.book.action.BookAction"></action>
    <action name="student" class="com.lxit.book.action.StudentAction"></action>
	<action name="user" class="com.lxit.book.action.UserAction">
	   <forward name="query" >/WEB-INF/jsp/user/user.jsp</forward>
	</action>
	<action name="dictionary" class="com.lxit.book.action.DictionaryAction"></action> 
	<action name="borrowBook" class="com.lxit.book.action.BorrowBookAction"></action>
	<action name="role" class="com.lxit.book.action.RoleAction">
	  <forward name="query" >/WEB-INF/jsp/role/role.jsp</forward>
	</action>
	<action name="purview" class="com.lxit.book.action.PurviewAction">
	  <forward name="query" >/WEB-INF/jsp/purview/purview.jsp</forward>
	</action>
</config>
  

 config.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<config>
	<action name="purview" class="com.lxit.book.action.PurviewAction">
	  <forward name="query" >/WEB-INF/jsp/purview/purview.jsp</forward>
	</action>
</config>
  

 

递归读取并解释多配置文件


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论