Search in sources :

Example 1 with Forward

use of com.weicoder.web.annotation.Forward in project weicoder by wdcode.

the class BasicServlet method result.

private void result(Method method, Object action, Object res, String callback, HttpServletRequest request, HttpServletResponse response, String ip, String actionName, Object[] params, Parameter[] pars, long curr) {
    try {
        // 判断是否需要写cookie
        boolean cookie = method.isAnnotationPresent(Cookies.class) || action.getClass().isAnnotationPresent(Cookies.class);
        String[] names = null;
        Cookies c = null;
        if (cookie) {
            // 获得Cookies注解
            c = method.getAnnotation(Cookies.class);
            if (c == null)
                c = action.getClass().getAnnotation(Cookies.class);
            names = c.names();
        }
        // 判断是否跳转url
        if (method.isAnnotationPresent(Redirect.class) || action.getClass().isAnnotationPresent(Redirect.class)) {
            String url = W.C.toString(res);
            if (U.E.isEmpty(url))
                ResponseUtil.json(response, callback, "Redirect is null");
            else
                response.sendRedirect(url);
        }
        // 是否Forward跳转
        if (method.isAnnotationPresent(Forward.class) || action.getClass().isAnnotationPresent(Forward.class)) {
            String url = W.C.toString(res);
            if (U.E.isEmpty(url))
                ResponseUtil.json(response, callback, "Forward is null");
            else
                request.getRequestDispatcher(url).forward(request, response);
        }
        // 是否json
        if (method.isAnnotationPresent(Json.class) || action.getClass().isAnnotationPresent(Json.class)) {
        } else if (WebParams.STATE || method.isAnnotationPresent(State.class) || action.getClass().isAnnotationPresent(State.class)) {
            // 状态码对象
            State state = method.getAnnotation(State.class);
            if (state == null)
                state = action.getClass().getAnnotation(State.class);
            // 字段名
            String code = state == null ? "code" : state.code();
            String content = state == null ? "content" : state.content();
            String message = state == null ? "message" : state.message();
            // 如果res为状态码
            if (res == null)
                // 写空信息
                res = Maps.newMap(new String[] { code, message }, StateCode.NULL.to());
            else if (res instanceof StateCode)
                // 写错误信息
                res = Maps.newMap(new String[] { code, message }, ((StateCode) res).to());
            else
                // 写入到前端
                res = Maps.newMap(new String[] { code, content }, new Object[] { StateCode.SUCCESS.getCode(), res });
        } else {
            // 如果结果为空
            if (res == null)
                // 结果设置为空map
                res = Maps.emptyMap();
        }
        // 是否写cookie
        if (cookie)
            CookieUtil.adds(response, c.maxAge(), res, names);
        // 写到前端
        LOG.info("request ip={} name={} time={} params={} pars={} method={} type={} res={} end", ip, actionName, System.currentTimeMillis() - curr, params, pars, request.getMethod(), request.getContentType(), ResponseUtil.json(response, callback, res));
    } catch (Exception e) {
        Logs.error(e);
    }
}
Also used : Cookies(com.weicoder.web.annotation.Cookies) State(com.weicoder.web.annotation.State) StateCode(com.weicoder.common.bean.StateCode) Forward(com.weicoder.web.annotation.Forward) Redirect(com.weicoder.web.annotation.Redirect) Json(com.weicoder.web.annotation.Json) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) StateException(com.weicoder.common.exception.StateException)

Aggregations

StateCode (com.weicoder.common.bean.StateCode)1 StateException (com.weicoder.common.exception.StateException)1 Cookies (com.weicoder.web.annotation.Cookies)1 Forward (com.weicoder.web.annotation.Forward)1 Json (com.weicoder.web.annotation.Json)1 Redirect (com.weicoder.web.annotation.Redirect)1 State (com.weicoder.web.annotation.State)1 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1