最近使用jfinal框架和dwz前端框架作一個後臺管理的系統。html
首先,不得不說jfinal和dwz都是很優秀的框架,在實際的開發中提供了很多的便利,向做者們致敬,之後會更深刻框架代碼進行學習!前端
這兩個框架上手着實很快,看了demo和教程文檔基本就能夠開始進行開發。然而,水平有限,前期寫了很多不合理的代碼,幾天在作後期的一些調整。例如以下的json數據渲染的問題...java
dwz Ajax表單提交後服務器端須要返回如下的json代碼:json
{ "statusCode":"200", "message":"操做成功", "navTabId":"", "rel":"", "callbackType":"closeCurrent", "forwardUrl":"" }
開始時,在controller的方法中,直接使用renderJson(String jsonText)將json直接以字符串的形式寫入,因此每一處須要渲染json的地方代碼以下般冗長不堪:前端框架
renderJson("{\"statusCode\":\"200\",\"message\":\"保存成功!\",\"navTabId\":\"navTabId\",\"rel\":\"\",\"callbackType\":\"closeCurrent\",\"forwardUrl\":\"\",\"confirmMsg\":\"\"}");
實際上,將這一部分進行封裝,即可以「一勞永逸」,代碼也會相對簡潔好看些。服務器
手寫一個類繼承Render類,根據實際狀況寫入數據域和方法,以下:框架
package com.car.admin.util; import java.io.IOException; import java.io.PrintWriter; import java.text.MessageFormat; import com.jfinal.render.Render; import com.jfinal.render.RenderException; @SuppressWarnings("serial") public class DwzRenderJson extends Render { private String statusCode = "200"; private String message = ""; private String navTabId = ""; private String callbackType = ""; private String forwardUrl = ""; private String rel=""; private String confirmMsg=""; public DwzRenderJson(String message, String navTabId, String callbackType) { this.message = message; this.navTabId = navTabId; this.callbackType = callbackType; } public DwzRenderJson() { } public static DwzRenderJson success() { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.setMessage("操做成功"); return dwzRenderJson; } public static DwzRenderJson success(String successMsg) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.setMessage(successMsg); return dwzRenderJson; } public static DwzRenderJson success(String successMsg,String navTabId) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.setMessage(successMsg); dwzRenderJson.setNavTabId(navTabId); return dwzRenderJson; } public static DwzRenderJson error() { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.statusCode = "300"; dwzRenderJson.message = "操做失敗"; return dwzRenderJson; } public static DwzRenderJson error(String errorMsg) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.statusCode = "300"; dwzRenderJson.message = errorMsg; return dwzRenderJson; } public static Render refresh(String refreshNavTabId) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.navTabId = refreshNavTabId; return dwzRenderJson; } public static DwzRenderJson closeCurrentAndRefresh(String refreshNavTabId) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.navTabId = refreshNavTabId; dwzRenderJson.callbackType = "closeCurrent"; return dwzRenderJson; } public static DwzRenderJson closeCurrentAndFoward(String refreshNavTabId, String fowardUrl) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.navTabId = refreshNavTabId; dwzRenderJson.callbackType = "closeCurrent"; dwzRenderJson.forwardUrl = fowardUrl; return dwzRenderJson; } public static DwzRenderJson closeCurrentAndRefresh(String refreshNavTabId, String message) { DwzRenderJson dwzRenderJson = new DwzRenderJson(); dwzRenderJson.navTabId = refreshNavTabId; dwzRenderJson.message = message; dwzRenderJson.callbackType = "closeCurrent"; return dwzRenderJson; } @Override public void render() { PrintWriter writer = null; String dwz = "\"statusCode\":\"{0}\",\"message\":\"{1}\",\"navTabId\":\"{2}\",\"rel\":\"{3}\",\"callbackType\":\"{4}\",\"forwardUrl\":\"{5}\",\"confirmMsg\":\"{6}\""; dwz = "{\n" + MessageFormat.format(dwz, statusCode, message, navTabId, rel, callbackType, forwardUrl , confirmMsg) + "\n}"; System.out.println(dwz); try { System.out.println(dwz); response.setHeader("Pragma", "no-cache"); // HTTP/1.0 caches might not implement Cache-Control and might only implement Pragma: no-cache response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); response.setContentType("text/html;charset=" + getEncoding()); //response.setCharacterEncoding(super.getEncoding()); writer = response.getWriter(); writer.write(dwz); writer.flush(); } catch (IOException e) { throw new RenderException(e); } finally { writer.close(); } } public String getStatusCode() { return statusCode; } public void setStatusCode(String statusCode) { this.statusCode = statusCode; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getNavTabId() { return navTabId; } public void setNavTabId(String navTabId) { this.navTabId = navTabId; } public String getCallbackType() { return callbackType; } public void setCallbackType(String callbackType) { this.callbackType = callbackType; } public String getForwardUrl() { return forwardUrl; } public void setForwardUrl(String forwardUrl) { this.forwardUrl = forwardUrl; } public String getRel() { return rel; } public void setRel(String rel) { this.rel = rel; } public String getConfirmMsg() { return confirmMsg; } public void setConfirmMsg(String confirmMsg) { this.confirmMsg = confirmMsg; } }
原來渲染json部分的代碼便爲以下:ide
render(DwzRenderJson.closeCurrentAndRefresh("storelist", "保存成功!"));
確實簡短了很多,也達到了想要的效果。
學習
水平實在有限,接觸實際開發也不久,經驗十分有限,不免要走一些彎路,可是,重要的仍是要勤于思考動手、多多請教學習。this