dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在SourceForge上找到它.
对主流的Java XML API进行的性能、功能和易用性的评测,dom4j无论在那个方面都是非常出色的。如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,例如Hibernate,包括sun公司自己的JAXM也用了Dom4j。
下面用dom4j来解析mystruts.xml。
xml代码
<?xml version="1.0" encoding="UTF-8"?>
<mystruts>
<package>
<action name="login" class="cn.albin.framework.action.LoginAction" method="login">
<result name="loginSuccess" type="redirect">/index.jsp</result>
<result name="loginFaild">/login.jsp</result>
</action>
<action name="register" class="cn.albin.framework.action.RegisterAction" method="register">
<result name="registerSuccess">/login</result>
</action>
</package>
</mystruts>
Result实体类
/**
* 封装结果视图
* <result name="success" type="redirect">/index.jsp</result>
*
*/
public class Result {
// 跳转的结果标记
private String name;
// 跳转类型,默认为转发; "redirect"为重定向
private String type;
// 跳转的页面
private String page;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPage() {
return page;
}
public void setPage(String page) {
this.page = page;
}
}
ActionMapping实体类
import java.util.Map;
/**
* 封装action节点
* <action name="login" class="cn.albin.framework.action.LoginAction" method="login">
<result name="success" type="redirect">/index.jsp</result>
<result name="loginFaild">/login.jsp</result>
</action>
*
*/
public class ActionMapping {
// 请求路径名称
private String name;
// 处理aciton类的全名
private String className;
// 处理方法
private String method;
// 结果视图集合
private Map<String,Result> results;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public Map<String, Result> getResults() {
return results;
}
public void setResults(Map<String, Result> results) {
this.results = results;
}
}
ActionMappingManager实体类
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* 加载配置文件, 封装所有的整个mystruts.xml
*
*
*/
public class ActionMappingManager {
// 保存action的集合
private Map<String,ActionMapping> allActions ;
public ActionMappingManager(){
allActions = new HashMap<String,ActionMapping>();
// 初始化
this.init();
}
/**
* 根据请求路径名称,返回Action的映射对象
* @param actionName 当前请求路径
* @return 返回配置文件中代表action节点的AcitonMapping对象
*/
public ActionMapping getActionMapping(String actionName) {
if (actionName == null) {
throw new RuntimeException("传入参数有误,请查看struts.xml配置的路径。");
}
ActionMapping actionMapping = allActions.get(actionName);
if (actionMapping == null) {
throw new RuntimeException("路径在struts.xml中找不到,请检查");
}
return actionMapping;
}
// 初始化allActions集合
private void init() {
/********DOM4J读取配置文件***********/
try {
// 1. 得到解析器
SAXReader reader = new SAXReader();
// 得到src/mystruts.xml 文件流
InputStream inStream = this.getClass().getResourceAsStream("/mystruts.xml");
// 2. 加载文件
Document doc = reader.read(inStream);
// 3. 获取根
Element root = doc.getRootElement();
// 4. 得到package节点
Element ele_package = root.element("package");
// 5. 得到package节点下, 所有的action子节点
List<Element> listAction = ele_package.elements("action");
// 6.遍历 ,封装
for (Element ele_action : listAction) {
// 6.1 封装一个ActionMapping对象
ActionMapping actionMapping = new ActionMapping();
actionMapping.setName(ele_action.attributeValue("name"));
actionMapping.setClassName(ele_action.attributeValue("class"));
actionMapping.setMethod(ele_action.attributeValue("method"));
// 6.2 封装当前aciton节点下所有的结果视图
Map<String,Result> results = new HashMap<String, Result>();
// 得到当前action节点下所有的result子节点
Iterator<Element> it = ele_action.elementIterator("result");
while (it.hasNext()) {
// 当前迭代的每一个元素都是 <result...>
Element ele_result = it.next();
// 封装对象
Result res = new Result();
res.setName(ele_result.attributeValue("name"));
res.setType(ele_result.attributeValue("type"));
res.setPage(ele_result.getTextTrim());
// 添加到集合
results.put(res.getName(), res);
}
// 设置到actionMapping中
actionMapping.setResults(results);
// 6.x actionMapping添加到map集合
allActions.put(actionMapping.getName(), actionMapping);
}
} catch (Exception e) {
throw new RuntimeException("启动时候初始化错误",e);
}
}
}