欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

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

系統 1776 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元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 国产免费资源 | 丁香花婷婷| 日日操天天操夜夜操 | 日韩福利在线观看 | 国产在线精品观看 | 欧美77| 日本视频免费 | 男女激情免费视频 | 日本久久久久久 | 91av一区| 亚洲国产高清高潮精品美女 | 91青青青国产在观免费影视 | 在线播放av片 | 亚洲精品久久久久无码AV片软件 | 午夜丁香 | 国产一区二区三区视频 | 影音先锋中文字幕一区 | 大逼逼影院| 国产视频首页 | 日本视频久久 | 欧美大片网站 | 人人综合网 | 免费成人在线网站 | 日日操av| 婷婷六月天 | 久久成人免费 | 久久丁香| 欧美日韩在线视频观看 | 国产精品高清m3u8在线播放 | 亚洲欧美在线观看一区二区 | 久久国产一区 | 日韩欧美视频在线 | 91短视频app下载 | 亚洲一级在线观看 | 亚洲一区二区三区免费视频 | 五月天国产视频 | 国产精品福利短视在线播放频 | 美女久草| 黄片毛片免费观看 | 亚洲无毛 | 性夜影院爽黄a爽在线看香蕉 |