(转) jfinal渲染dwz所需格式的json类封装

最近使用jfinal框架和dwz前端框架做一个后台管理的系统。

首先,不得不说jfinal和dwz都是很优秀的框架,在实际的开发中提供了不少的便利,向作者们致敬,以后会更深入框架代码进行学习!

这两个框架上手着实很快,看了demo和教程文档基本就可以开始进行开发。然而,水平有限,前期写了不少不合理的代码,几天在做后期的一些调整。例如如下的json数据渲染的问题...

dwz Ajax表单提交后服务器端需要返回以下的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;
	}

}

原地址:http://www.fanli7.net/a/bianchengyuyan/JS/2014/0323/440142.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值