模拟Struts读取XML,利用反射机制执行Action

Struts框架是著名的框架,今天我就简单谈一谈它,并在本地写一个小程序去模拟解析Struts.xml并尝试用反射机制去执行Action

通过如下模拟小程序,对Java的反射机制有了一定的了解

1.解析Struts.xml,如下XML文件

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <struts>
 3     <action name="login" class="com.coderising.litestruts.action.LoginAction">
 4         <result name="success">/jsp/homepage.jsp</result>
 5         <result name="fail">/jsp/showLogin.jsp</result>
 6     </action>
 7     <action name="logout" class="com.coderising.litestruts.action.LogoutAction">
 8         <result name="success">/jsp/welcome.jsp</result>
 9         <result name="error">/jsp/error.jsp</result>
10     </action>
11 </struts>

 1.1.根据action的name,得到对应的类名

  i.解析XML的技术

    有很多现存解析XML的库,dom4j等等,我个人用的JDK1.8自带的org.w3d.dom包里面提供的API

    1)根据相对路径拿到Struts XML对应的File对象

    private File lookupConfigFile(){
        URL url = getClass().getResource(CONFIG_PATH);
        return new File(url.getPath());
    }

    2)根据File对象,把它解析成一个Document对象

    private Document generateDoc(){
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        Document doc = null;
        try {
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            doc = docBuilder.parse (lookupConfigFile());
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return doc;
    }

     3)解析action节点,并根据传入的action,得到对应的类名

    public String invokedAction(String actionName){
        if(null != actionName && !"".equals(actionName)){
            Document doc = generateDoc();
            NodeList nodeList = doc.getElementsByTagName(CONFIG_NODE_ACTION);
            for (int i = 0; i < nodeList.getLength(); i++) {
                String actionNameConfiged = nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_NAME).getNodeValue();
                if(actionName.equals(actionNameConfiged)){
                    return nodeList.item(i).getAttributes().getNamedItem(CONFIG_ATTR_CLASS).getNodeValue();
                }
            }
        }
        throw new RuntimeException("actionName can't be found");
    }

 1.2.利用反射类名,执行传入参数的Setter方法

  反射能够得到一个类的相关信息

Class<?> actionClass = Class.forName(className);

  i.Method

  1)定义调用的方法

Method setNameMethod = actionClass.getMethod("setName", String.class);

  2)生成实体类

Object action = actionClass.newInstance();

  3)调用方法,并把实体类传入

 setNameMethod.invoke(action, name);
 setPasswordMethod.invoke(action, password);

 1.3.调用execute方法,并拿到要显示的Message

Method executeMethod = actionClass.getMethod("execute");
executeMethod.invoke(action).toString()

1.4.根据拿到的Message,封装一个简单对象返回

可以参考具体分享的代码

 

具体详细的代码可以参考如下地址:

 https://github.com/conf1102/coding2017/tree/master/group20/404130810/src/com/coderising/litestruts

转载于:https://www.cnblogs.com/alvinwu/p/6505926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值