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;
                    }
                }

  • 相关阅读:
    VS2012快捷操作功能
    SQL Union和SQL Union All用法
    安装sql server 2008 R2出现 创建usersettings/microsoft.sqlserver.configuration.landingpage.properties.setter
    编码的来历和使用 utf-8 和GB2312比较
    Java内存机制,内存地址
    spring
    使用CMD建立指定格式的文件
    在命令提示符窗口下(cmd)使用指令操作并编译java代码,运行java编译代码
    匿名内部类的使用总结
    网页中插入FLASH(swf文件)的html代码
  • 原文地址:https://www.cnblogs.com/zyh1994/p/5598289.html
Copyright © 2011-2022 走看看