第5章 优化Action参数

     有些地方用不到参数,比如进入某个页面,不用接收参数为什么要定义Param呢,现在就来优化这个问题。

     原来是每次请求都针对的简历了一个param对象,放入action参数中,可以这样处理:在DispatcherServlet中首先判断param是否为null即可。

     红色部分为修改的部分。

Param param = new Param(paramMap);

Object result; Method actionMethod = handler.getActionMethod();
if (param.isEmpty()) {
    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod);
} else {
    result = ReflectionUtil.invokeMethod(controllerBean, actionMethod, param);
}

//处理Action方法返回值
if (result instanceof View) {
    handleViewResult((View) result, request, response);
} else if (result instanceof Data) {
    handleDataResult((Data) result, response);
}

     要在Param类中添加isEmpty方法。

/**
 * 验证参数是否为空
 */
public boolean isEmpty(){
    return CollectionUtil.isEmpty(paramMap);
}

     此时,在Service方法中可以省略Param的地方就都可以省略了。

你可能感兴趣的:(第5章 优化Action参数)