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