Search in sources :

Example 1 with BaseController

use of com.duangframework.mvc.core.BaseController in project duangframework by tcrct.

the class ActionHandle method execute.

@Override
public void execute(String target, IRequest request, IResponse response) throws Exception {
    // 请求的URL中如果包含有.  则全部当作是静态文件的请求处理,直接返回
    if (target.contains(".")) {
        return;
    }
    // 分号后的字符截断
    if (target.contains(";")) {
        target = target.substring(0, target.indexOf(";"));
    }
    // 暂不支持根目录请求
    if ("/".equals(target)) {
        return;
    }
    Action action = InstanceFactory.getActionMapping().get(target);
    if (null == action) {
        action = getRestfulActionMapping(request, target);
        if (null == action) {
            throw new DuangMvcException("action is null or access denied");
        }
    }
    Class<?> controllerClass = action.getControllerClass();
    BaseController controller = null;
    // 是否单例
    if (action.isSingleton()) {
        controller = (BaseController) BeanUtils.getBean(action.getControllerClass(), controllerClass);
    } else {
        // 如果不是设置为单例模式的话就每次请求都创建一个新的Controller对象
        controller = ClassUtils.newInstance(controllerClass);
        // 还要重新执行Ioc注入
        IocHelper.ioc(controller.getClass());
    }
    // 传入request, response到Controller
    controller.init(request, response);
    // 取出方法对象
    Method method = action.getMethod();
    // 取消类型安全检测(可提高反射性能)
    method.setAccessible(true);
    // 反射执行方法
    method.invoke(controller, NULL_ARGS);
    // 返回结果
    controller.getRender().setContext(request, response).render();
}
Also used : Action(com.duangframework.mvc.core.Action) BaseController(com.duangframework.mvc.core.BaseController) Method(java.lang.reflect.Method) DuangMvcException(com.duangframework.core.exceptions.DuangMvcException)

Aggregations

DuangMvcException (com.duangframework.core.exceptions.DuangMvcException)1 Action (com.duangframework.mvc.core.Action)1 BaseController (com.duangframework.mvc.core.BaseController)1 Method (java.lang.reflect.Method)1