📄 actionconfig.java
字号:
package com.xaccp.aj3q8073.controller;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 从配置文件xml中解析出来的 所有类信息并且存储在 HashMap中
*
* @author Administrator
*
*/
public class ActionConfig {
// 利用HashMap以键值对的形式存储path和action
static HashMap<String, ActionMapping> mapping = new HashMap<String, ActionMapping>();
public static void putMapping(ActionMapping actionMapping) {
mapping.put(actionMapping.getPath(), actionMapping);
}
/**
* 根据放在HashMap中的键取值
*
* @param path
* @return
*/
public static ActionMapping getActionMapping(String servletPath) {
return mapping.get(servletPath);
}
/**
* 解析配置文件
*
* @param configPath
*/
public static void init(String configPath) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(configPath));
Element root = doc.getDocumentElement();
NodeList list = root.getChildNodes();
// 从根目录开始找到第一层的ELEMENT_NODE
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
if (node.getNodeType() != Node.ELEMENT_NODE) {
continue;
}
// 根据节点名称判断
if (node.getNodeName() == "parament") {
NodeList lis = node.getChildNodes();
// 从第一层开始找到第二层的ELEMENT_NODE
for (int j = 0; j < lis.getLength(); j++) {
if (!(lis.item(j) instanceof Element)) {
continue;
}
Element action = (Element) lis.item(j);
// 根据属性名找到它所对应的值
String path = action.getAttribute("path");
String className = action.getAttribute("class");
String methodName = action.getAttribute("method");
// 判断方法
if (methodName == null || methodName.length() == 0) {
methodName = "execute";
}
// 根据全定路径加载类
Class c = Class.forName(className);
// 调用构造方法分配内存空间生成类的实例
Object obj = c.newInstance();
// 把解析到的类信息放进ActionMapping中
ActionMapping mapping = new ActionMapping();
mapping.setPath(path);
mapping.setAction(obj);
mapping.setMethodName(methodName);
// 最后再把ActionMapping放进HashMap中
putMapping(mapping);
}
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -