?最近做項(xiàng)目時(shí),遇到了多配置文件讀取的問題。最后,還是采用了遞歸讀取配置文件的方法去實(shí)現(xiàn),感覺挺實(shí)用的。
?
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>
?
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
