在开发过程中,抽象成模型,定义规范是非常有必要的,不仅可以简化代码,提高开发效率,也为自己减少了不少麻烦。
在开发中,因为DWZ这块是我负责,由于代码琐碎,重复度高,没有抽象封装,没有定义规范,别人不清楚都来问我,导致我经常在解决相同的问题,所以我把DWZ中的信息回馈这块抽象封装成通用模型,便于使用,也减少了自己的麻烦。
DWZ是客户端框架,自然需要接收后台传来的消息,可能是异常信息,可能是成功信息。
返回信息是json格式,其中包括状态码、callbackType类型、navTabId等。
AjaxObject.java
packagecom.dwz.util;
/**
*
* callbackType如果是closeCurrent就会关闭当前tab
*只有callbackType="forward"时需要forwardUrl值
*
*form提交后返回json数据结构statusCode=DWZ.statusCode.ok表示操作成功, 做页面跳转等操作.
* statusCode=DWZ.statusCode.error表示操作失败,提示错误原因.
*statusCode=DWZ.statusCode.timeout表示session超时,下次点击时跳转到DWZ.loginUrl
* {"statusCode":"200","message":"操作成功","navTabId":"navNewsLi","forwardUrl":"","callbackType":"closeCurrent"}
* {"statusCode":"300","message":"操作失败"}
* {"statusCode":"301","message":"会话超时"}
*
* @author <ahref="mailto:[email protected]">tch</a>
* Version 1.0.0
* @since 2013-12-8 下午6:46:53
*/
publicclass AjaxObject {
//状态码
publicfinal static int STATUS_CODE_SUCCESS = 200;
publicfinal static int STATUS_CODE_FAILURE = 300;
publicfinal static int STATUS_CODE_TIMEOUT = 301;
publicfinal static int STATUS_CODE_FORBIDDEN = 403;
//callbackType类型
publicfinal static String CALLBACK_TYPE_CLOSE_CURRENT = "closeCurrent";
publicfinal static String CALLBACK_TYPE_FORWARD = "forward";
privateint statusCode = STATUS_CODE_SUCCESS;
privateString message = "";
privateString navTabId = "";
privateString forwardUrl = "";
privateString rel = "";
privateString callbackType = CALLBACK_TYPE_CLOSE_CURRENT;
publicAjaxObject() {
}
publicAjaxObject(String message) {
this.message= message;
}
/**
* 构造函数
* @param statusCode
*/
publicAjaxObject(int statusCode) {
super();
this.statusCode= statusCode;
}
/**
* 构造函数
* @param statusCode
* @param message
*/
publicAjaxObject(int statusCode, String message) {
super();
this.statusCode= statusCode;
this.message= message;
}
/**
* 构造函数
* @param statusCode
* @param message
* @param callbackType
*/
publicAjaxObject(int statusCode, String message, String callbackType) {
this.statusCode= statusCode;
this.message= message;
this.callbackType= callbackType;
}
/**
* 构造函数
* @param statusCode
* @param message
* @param forwardUrl
* @param rel
* @param callbackType
*/
publicAjaxObject(int statusCode, String message, String navTabId, String forwardUrl,
Stringrel, String callbackType) {
this.statusCode= statusCode;
this.message= message;
this.navTabId= navTabId;
this.forwardUrl= forwardUrl;
this.rel= rel;
this.callbackType= callbackType;
}
/**
* 返回 statusCode 的值
* @return statusCode
*/
publicint getStatusCode() {
returnstatusCode;
}
/**
* 设置 statusCode 的值
* @param statusCode
*/
publicAjaxObject setStatusCode(int statusCode) {
this.statusCode= statusCode;
returnthis;
}
/**
* 返回 message 的值
* @return message
*/
publicString getMessage() {
returnmessage;
}
/**
* 设置 message 的值
* @param message
*/
publicAjaxObject setMessage(String message) {
this.message= message;
returnthis;
}
/**
* 返回 forwardUrl 的值
* @return forwardUrl
*/
publicString getForwardUrl() {
returnforwardUrl;
}
/**
* 设置 forwardUrl 的值
* @param forwardUrl
*/
publicAjaxObject setForwardUrl(String forwardUrl) {
this.forwardUrl= forwardUrl;
returnthis;
}
/**
* 返回 rel 的值
* @return rel
*/
publicString getRel() {
returnrel;
}
/**
* 设置 rel 的值
* @param rel
*/
publicAjaxObject setRel(String rel) {
this.rel= rel;
returnthis;
}
/**
* 返回 callbackType 的值
* @return callbackType
*/
publicString getCallbackType() {
returncallbackType;
}
/**
* 设置 callbackType 的值
* @param callbackType
*/
publicAjaxObject setCallbackType(String callbackType) {
this.callbackType= callbackType;
returnthis;
}
/**
* 返回 navTabId 的值
* @return navTabId
*/
publicString getNavTabId() {
returnnavTabId;
}
/**
* 设置 navTabId 的值
* @param navTabId
*/
publicAjaxObject setNavTabId(String navTabId) {
this.navTabId= navTabId;
returnthis;
}
publicstatic AjaxObject newOk(String message) {
returnnew AjaxObject(STATUS_CODE_SUCCESS, message);
}
publicstatic AjaxObject newError(String message) {
returnnew AjaxObject(STATUS_CODE_FAILURE, message);
}
publicstatic AjaxObject newTimeout(String message) {
returnnew AjaxObject(STATUS_CODE_TIMEOUT, message);
}
publicstatic AjaxObject newForbidden(String message) {
returnnew AjaxObject(STATUS_CODE_TIMEOUT, message);
}
publicstatic AjaxObject newRefreshNavtab(String navTabId, String message) {
AjaxObjectajaxObject = new AjaxObject(message);
ajaxObject.navTabId= navTabId;
returnajaxObject;
}
publicstatic AjaxObject newRefreshRel(String rel, String message) {
AjaxObjectajaxObject = new AjaxObject(message);
ajaxObject.rel= rel;
returnajaxObject;
}
publicstatic AjaxObject newForward(String forwardUrl) {
AjaxObjectajaxObject = new AjaxObject(CALLBACK_TYPE_FORWARD);
ajaxObject.forwardUrl= forwardUrl;
returnajaxObject;
}
/**
* 转换成JSONObject,以便传到客户端显示
* @return
* @see java.lang.Object#toString()
*/
@Override
publicString toString() {
StringBufferbuffer = new StringBuffer();
buffer.append("{")
.append("\"statusCode\":\"" + statusCode +"\",")
.append("\"message\":\"" + message +"\",")
.append("\"navTabId\":\"" + navTabId +"\",")
.append("\"rel\":\""+ rel + "\",")
.append("\"callbackType\":\"" + callbackType +"\",")
.append("\"forwardUrl\":\"" + forwardUrl +"\"")
.append("}");
returnbuffer.toString();
}
}
重点
通过AjaxObject的toString方法,将信息转换成JSONObject,返回到客户端显示
userService.java
public void addUser(User user) throws Exception {
//测试异常
if("tch".equals(user.getName())) {
thrownew Exception("已经存在该用户!");
}
userAction.java
publicvoid userSave(){
Stringmsg;
try{
userDao.saveorupdate(user);
outMsg(AjaxObject.newOk("添加用户成功啦!").setNavTabId("userListview").toString());
}catch (Exception e) {
msg= AjaxObject.newError(e.getMessage()).setCallbackType("").toString();
}
//将处理信息发送到客户端
HttpServletResponseresponse=ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
try{
PrintWriterout=response.getWriter();
out.print(msg);
out.close();
}catch (IOException e) {
e.printStackTrace();
}
}
效果图