配置文件的封装:
/login.jsp /index.jsp /index.jsp
如果对这个配置文件进行封装,采用由里到外的思想,层层封装。
result---》action----》package
对result封装
package cn.lyjs.framework.bean;public class Result { 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; } //跳转的结果标记 private String name; //跳转的类型 转发 重定向 private String type; //跳转的页面 private String page;}
对Action的封装
package cn.lyjs.framework.bean;import java.util.Map;/** * 封装action节点 ** @author Lavender * */public class ActionMapping { //请求路径名称 private String className; //处理action类的全名 private String classPath; //处理方法 private String method; //结果视图集合 private Map results; public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getClassPath() { return classPath; } public void setClassPath(String classPath) { this.classPath = classPath; } public String getMethod() { return method; } public void setMethod(String method) { this.method = method; } public Map getResults() { return results; } public void setResults(Map results) { this.results = results; }}
所有Action加入集合
package cn.lyjs.framework.bean;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;public class ActionMappingManager { private MapallActions; public ActionMappingManager(){ allActions = new HashMap (); // 初始化 this.init(); } 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; } private void init(){ try { SAXReader reader=new SAXReader(); InputStream inputStream=this.getClass().getResourceAsStream("/mystruts.xml"); Document docment= reader.read(inputStream); Element root=docment.getRootElement(); Element packageList=root.element("package"); // for(Element list: packageList){ List listAction=packageList.elements("action"); for(Element ele_action:listAction){ ActionMapping actionMapping=new ActionMapping(); actionMapping.setClassName(ele_action.attributeValue("name")); actionMapping.setClassPath(ele_action.attributeValue("class")); actionMapping.setMethod(ele_action.attributeValue("method")); Map results=new HashMap (); Iterator i=ele_action.elementIterator("result"); while(i.hasNext()){ Element ele_result=(Element) i.next(); Result result=new Result(); result.setName(ele_result.attributeValue("name")); result.setType(ele_result.attributeValue("type")); result.setPage(ele_result.getTextTrim()); results.put(result.getName(), result); } actionMapping.setResults(results); allActions.put(actionMapping.getClassName(), actionMapping); } // } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}