2013年(92)
分类: 信息化
2013-03-29 02:38:06
[代码] [Java]代码 package com.exam.control; import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.dom4j.Document; import org.dom4j.Element ; import org.dom4j.io.SAXReader; /** * 解析mystruts.xml文件 * @author wang * */ public class ActionMappingManager { /** * 保管全部action的ActionMapping */ private static Map actionMapping=new HashMap(); /** * 此布局方法:根据Action装备文件名称,初始化Action方位文件 * @param configMyStrutsName 装备文件名称数组 */ public ActionMappingManager(String[]configMyStrutsName){ for(String str:configMyStrutsName){ initMyStrutsXml(str); } } /** * 加载并解析action装备文件 * @param configMyStrutsFileName 装备文件名称 */ @SuppressWarnings("unchecked") public void initMyStrutsXml(String configMyStrutsFileName){ try { if(configMyStrutsFileName == null || configMyStrutsFileName.isEmpty()){ throw new Exception("装备文件名不能为空"); } //根据装备文件方法加载到输入流里 InputStream is = this.getClass().getResourceAsStream("/" configMyStrutsFileName); //用dom4j 读取xml文件 Document doc = new SAXReader().read(is); Element root = doc.getRootElement();//获取根方针 //迭代root Iterator actionsIt= root.elements("actions").iterator(); Element actions = actionsIt.next(); //获取actions里的action for(Iterator actionIt = actions.elementIterator("action");actionIt.hasNext();){ Element action = actionIt.next(); ActionMapping am = new ActionMapping(); am.setName(action.attributeValue("name")); am.setClassUrl(action.attributeValue("class")); //遍历全部action的子元素result for(Iterator resultLt= action.elementIterator("result");resultLt.hasNext();){ Element resultElement=resultLt.next(); String name=resultElement.attributeValue("name"); //不写默认为success if(name==null || "".equals(name)){ name="success"; } Result result = new Result(); result.setName(name);//success result.setRedriect(Boolean.valueOf(resultElement.attributeValue("redirect")));//能否是redriect result.setUrl(resultElement.getText());//视图方法 //把每个封装好的result 添加到ActionMapping里 am.addResult(name, result); } actionMapping.put(am.getName(), am); } //Iterator actions = root.elements("actions").iterator(); } catch (Exception e) { e.printStackTrace(); } } /** * 根据actionName查询对应的ActionMapping * @param actionName * @return */ public ActionMapping getActionMappingByName(String actionName)throws Exception{ if(actionName == null || actionName.isEmpty()){ return null; } //查找集结 ActionMapping mapping = actionMapping.get(actionName); if(mapping == null){ throw new Exception("系统找不到:[" actionName "],请检查装备文件"); } return mapping; } }