zoukankan      html  css  js  c++  java
  • 模拟struts2

    利用到的技术:dom4j和xpath

    自己写一个Filter

    在doFilter中拦截请求

        // 2.1 得到请求资源路径
                String uri = request.getRequestURI();
                String contextPath = request.getContextPath();
                String path = uri.substring(contextPath.length() + 1);

                // System.out.println(path); // hello

                // 2.2 使用path去struts.xml文件中查找某一个<action name=path>这个标签
                SAXReader reader = new SAXReader();
                // 得到struts.xml文件的document对象。
                Document document = reader.read(new File(this.getClass()
                        .getResource("/struts.xml").getPath()));

                Element actionElement = (Element) document
                        .selectSingleNode("//action[@name='" + path + "']"); // 查找<action
                                                                                // name='hello'>这样的标签

                if (actionElement != null) {
                    // 得到<action>标签上的class属性以及method属性
                    String className = actionElement.attributeValue("class"); // 得到了action类的名称
                    String methodName = actionElement.attributeValue("method");// 得到action类中的方法名称。

                    // 2.3通过反射,得到Class对象,得到Method对象
                    Class actionClass = Class.forName(className);
                    Method method = actionClass.getDeclaredMethod(methodName);

                    // 2.4 让method执行.
                    String returnValue = (String) method.invoke(actionClass
                            .newInstance()); // 是让action类中的方法执行,并获取方法的返回值。

                    // 2.5
                    // 使用returnValue去action下查找其子元素result的name属性值,与returnValue做对比。
                    Element resultElement = actionElement.element("result");
                    String nameValue = resultElement.attributeValue("name");

                    if (returnValue.equals(nameValue)) {
                        // 2.6得到了要跳转的路径。
                        String skipPath = resultElement.getText();

                        // System.out.println(skipPath);

                        request.getRequestDispatcher(skipPath).forward(request,
                                response);
                        return;
                    }
                }

  • 相关阅读:
    应用环境配置记录
    【C#】Dictionary通过value获取对应的key值
    DevExpress 之 GridControl 自定义列(转)
    C#中gridView常用属性和技巧介绍(转)
    【643】cv2.imread() 函数
    【642】Python 实现膨胀、腐蚀、提取边线
    【639】keras 中 fit_generator 的 数据生成器
    【638】keras 多输出模型【实战】
    【637】一个图片两个标注的图像增强
    别自嗨了!想做新生代农民工,你还不够格。。
  • 原文地址:https://www.cnblogs.com/zyh1994/p/5598289.html
Copyright © 2011-2022 走看看