zoukankan      html  css  js  c++  java
  • 对xml文件封装思想的处理

    配置文件的封装:

    <?xml version="1.0" encoding="UTF-8"?>
    <mystruts>
            <action name="login" class="cn.lyjs.framework.action.LoginAction" method="login">
                <result name="loginFaild" >/login.jsp </result>
                <result name="loginSuccess" type="redirect">/index.jsp </result>
            </action>
            
            <action name="register" class="cn.lyjs.framework.action.RegisterAction" method="register">
                <result name="registerSuccess" type="redirect">/index.jsp </result>
            </action>
    </mystruts>

    如果对这个配置文件进行封装,采用由里到外的思想,层层封装。

    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节点
     *         <action name="login" class="" method="">
                <result name="" type=""> </result>
            </action>
     * @author Lavender
     *
     */
    public class ActionMapping {
        //请求路径名称
        private String className;
        //处理action类的全名
        private String classPath;
        //处理方法
        private String method;
        //结果视图集合
        private Map<String,Result> 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<String, Result> getResults() {
            return results;
        }
        public void setResults(Map<String, Result> 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 Map<String,ActionMapping> allActions;
        
        public ActionMappingManager(){
            allActions = new HashMap<String,ActionMapping>();
            // 初始化
            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<Element> 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<String,Result> results=new HashMap<String, Result>();
                        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();
            }
        }
    }
  • 相关阅读:
    94、二叉树的中序遍历 | JS
    102、二叉树的层序遍历 | JS
    111、二叉树的最小深度 | JS
    二叉树的先中后序遍历-JS非递归实现
    二叉树的先中后序遍历-JS递归实现
    深度和广度优先遍历-JS实现
    76、最小覆盖子串 | JS-字典
    extra1 二分查找与二叉判定树
    02 线性表的顺序存储
    原型、原型链、作用域、作用域链、闭包
  • 原文地址:https://www.cnblogs.com/lyjs/p/5084096.html
Copyright © 2011-2022 走看看