基于Struts2框架思想的小项目(三)

本文介绍了一个使用Struts框架进行单元测试的例子,包括了成功的登录测试和失败的登录测试两个场景,并详细阐述了如何通过反射机制来实现Struts框架的核心功能。

前面两讲我们都已经完成了所有的工具类,这样,我们就能站在更高层面上编写框架,还是按照老思路,我们先编写测试用的测试类,先编写用到的测试类:

public void testLoginActionSuccess() {

        String actionName = "login";

        Map<String,String> params = new HashMap<String,String>();
        params.put("name","test");
        params.put("password","1234");


        View view  = Struts.runAction(actionName,params);        

        Assert.assertEquals("/jsp/homepage.jsp", view.getJsp());
        Assert.assertEquals("login successful", view.getParameters().get("message"));
    }

public void testLoginActionFailed() {
        String actionName = "login";
        Map<String,String> params = new HashMap<String,String>();
        params.put("name","test");
        params.put("password","123456"); //密码和预设的不一致

        View view  = Struts.runAction(actionName,params);        

        Assert.assertEquals("/jsp/showLogin.jsp", view.getJsp());
        Assert.assertEquals("login failed,please check your user/pwd", view.getParameters().get("message"`);
    }

我们按照之前的框架流程,调用之前的方法类,可编写出runaction方法

public class Struts {

    private final static Configuration cfg = new Configuration("struts.xml");

    public static View runAction(String actionName, Map<String,String> parameters) {

        /*

        0. 读取配置文件struts.xml

        1. 根据actionName找到相对应的class , 例如LoginAction,   通过反射实例化(创建对象)
        据parameters中的数据,调用对象的setter方法, 例如parameters中的数据是 
        ("name"="test" ,  "password"="1234") ,      
        那就应该调用 setName和setPassword方法

        2. 通过反射调用对象的exectue 方法, 并获得返回值,例如"success"

        3. 通过反射找到对象的所有getter方法(例如 getMessage),  
        通过反射来调用, 把值和属性形成一个HashMap , 例如 {"message":  "登录成功"} ,  
        放到View对象的parameters

        4. 根据struts.xml中的 <result> 配置,以及execute的返回值,  确定哪一个jsp,  
        放到View对象的jsp字段中。

        */



        String clzName = cfg.getClassName(actionName);

        if(clzName == null){
            return null;
        }

        try {

            Class<?> clz = Class.forName(clzName);  
            //实例化目标类
            Object action = clz.newInstance();
            //封装数据
            ReflectionUtil.setParameters(action, parameters);
            //获取excute的方法
            Method m = clz.getDeclaredMethod("execute");
            //调用对应类该类的excute这个方法,这个方法会返回一个字符串
            String resultName = (String)m.invoke(action);
            //获得javabean中,我们指定的数据集合
            Map<String,Object> params = ReflectionUtil.getParamterMap(action);  
            // 根据struts.xml中的 <result> 配置,以及execute的返回值,  确定哪一个jsp
            String resultView = cfg.getResultView(actionName, resultName);
            //放到View对象的jsp字段中
            View view = new View();         
            view.setParameters(params);
            view.setJsp(resultView);
            return view;



        } catch (Exception e) {

            e.printStackTrace();
        }
        return null;
    }    

}

其中,在实现

// 根据struts.xml中的 <result> 配置,以及execute的返回值,  确定哪一个jsp
            String resultView = cfg.getResultView(actionName, resultName);

View view = new View();         
            view.setParameters(params);
            view.setJsp(resultView);

这三句代码的时候,需要在刚开始解析的XML那个Configuration类中增加下面的方法,这样,我们需要什么,就从集合和之前定义好的构造器里去寻找我们所需要的数据。

public String getResultView(String action, String resultName) {
        ActionConfig ac = this.actions.get(action);
        if(ac == null){
            return null;
        }
        return ac.getViewName(resultName);
    }
View这个构造器我们设计为如下所示:


import java.util.Map;

public class View {
    private String jsp;
    private Map parameters;

    public String getJsp() {
        return jsp;
    }
    public View setJsp(String jsp) {
        this.jsp = jsp;
        return this;
    }
    public Map getParameters() {
        return parameters;
    }
    public View setParameters(Map parameters) {
        this.parameters = parameters;
        return this;
    }
}

总结:
该项目内在逻辑要求比较强,需要在编写的时候严格明确所需要的逻辑关系,同时,在反射过程中一定要理清思路,不要被反射迷惑的方向,还要明白各个工具类所起到的作用    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值