Search in sources :

Example 6 with ModelAndView

use of com.blade.mvc.view.ModelAndView in project blade by biezhi.

the class RouteViewResolve method handle.

public void handle(Request request, Response response, Route route) throws Exception {
    try {
        Method actionMethod = route.getAction();
        Object target = route.getTarget();
        int len = actionMethod.getParameterTypes().length;
        Object returnParam;
        if (len > 0) {
            Object[] args = MethodArgument.getArgs(request, response, actionMethod);
            returnParam = ReflectKit.invokeMehod(target, actionMethod, args);
        } else {
            returnParam = ReflectKit.invokeMehod(target, actionMethod);
        }
        if (null != returnParam) {
            Class<?> returnType = returnParam.getClass();
            RestController restController = target.getClass().getAnnotation(RestController.class);
            JSON json = actionMethod.getAnnotation(JSON.class);
            if (null != restController || null != json) {
                response.json(viewSettings.toJSONString(returnParam));
            } else {
                if (returnType == String.class) {
                    response.render(returnParam.toString());
                } else if (returnType == ModelAndView.class) {
                    ModelAndView modelAndView = (ModelAndView) returnParam;
                    response.render(modelAndView);
                }
            }
        }
    } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        throw new BladeException(e.getCause());
    } catch (Exception e) {
        throw e;
    }
}
Also used : BladeException(com.blade.exception.BladeException) ModelAndView(com.blade.mvc.view.ModelAndView) RestController(com.blade.mvc.annotation.RestController) JSON(com.blade.mvc.annotation.JSON) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) BladeException(com.blade.exception.BladeException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

ModelAndView (com.blade.mvc.view.ModelAndView)6 FileItem (com.blade.mvc.multipart.FileItem)2 BladeException (com.blade.exception.BladeException)1 NotFoundException (com.blade.exception.NotFoundException)1 RouteException (com.blade.exception.RouteException)1 JSON (com.blade.mvc.annotation.JSON)1 RestController (com.blade.mvc.annotation.RestController)1 IOException (java.io.IOException)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1