刚刚为了做统一返回,自定义了一个
public class MyErrorRender extends ErrorRender { protected String msg ; public MyErrorRender(int errorCode, String view) { super(errorCode, view); } /** * JFinal 的Controller限制了发挥,下面的构造方法用不上! * <br>期盼以后可以用上吧 * @param msg * @param errorCode */ public MyErrorRender(String msg, int errorCode) { super(errorCode, null); this.msg = msg; } @Override public void render() { response.setStatus(getErrorCode()); // HttpServletResponse.SC_XXX_XXX PrintWriter writer = null; try { if(XX.isAjax(request) && XX.isJsonRender(request)){ RenderManager.me(); RenderManager.me().getRenderFactory().getJsonRender(getErrorJson()).setContext(request, response).render();; return; } // render with view String view = getView(); if (view != null) { RenderManager.me().getRenderFactory().getRender(view).setContext(request, response).render(); return; } // render with html content response.setContentType(contentType); writer = response.getWriter(); writer.write(getErrorHtml()); writer.flush(); } catch (IOException e) { throw new RenderException(e); } } /** * 获得返回的json格式数据 * @author WangWei * @created 2017-10-19 下午2:28:16 * @return */ private String getErrorJson() { int errorCode = super.getErrorCode(); msg = this.msg == null?errorCode + " 系统错误":this.msg; return JsonKit.toJson(Kv.by("errCode", errorCode).set("msg", msg)); } public int getErrorCode(){ int myCode = super.getErrorCode(); ArrayList<Integer> sysCode = new ArrayList<>(Arrays.asList(201,202,203,204, 301,302,303,304,305,306, 400,401,402,403,404,407,415, 500,501,502,503)); if(sysCode.contains(myCode)){ return myCode; }else{ return 200; } } }
如何获取控制器的方法setAttr设置的变量值?
已解决:原来是设置了request!