日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

遞歸讀取并解釋多配置文件

系統 1843 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條評論
主站蜘蛛池模板: 农安县| 渭南市| 永善县| 临夏市| 仁化县| 娱乐| 芮城县| 繁昌县| 铜陵市| 固安县| 威信县| 邵武市| 鄂尔多斯市| 维西| 墨竹工卡县| 于田县| 聊城市| 长宁区| 弥渡县| 新巴尔虎右旗| 余干县| 岗巴县| 定远县| 新津县| 汕头市| 武功县| 丰台区| 湘潭市| 昆明市| 精河县| 大丰市| 齐河县| 中山市| 安阳县| 炎陵县| 乌鲁木齐县| 永年县| 油尖旺区| 麻城市| 梁山县| 淄博市|