action中json的應用

來源:互聯網
上載者:User

這篇文章重點介紹action中json資料的返回處理;如果需要看前端代碼的一些特效或ajax的json接收,請看上一篇部落格:http://blog.csdn.net/yangkai_hudong/article/details/24422817

1.需要依賴的方法

/**

* 擷取PrintWriter

*

* @throws IOException

*/

public static PrintWritergetPrintWriter(HttpServletResponse response) throws IOException {

response.setContentType("text/plain;charset=utf-8");

response.setCharacterEncoding("UTF-8");

response.setHeader("Pragma","no-cache");

response.setHeader("Cache-Control","no-cache");

response.setDateHeader("Expires", 0);

PrintWriter out = response.getWriter();

return out;

}

/**

* 輸出json操作

*

* @param out

* PrintWriter

* @param result

*/

public voidresponseStr(PrintWriter out, String result) {

out.println(result);

out.flush();

out.close();

}

public String createJsonObject(Stringflag, String msg) {

JSONObject obj = new JSONObject();

try {

obj.put("flag", flag);

obj.put("msg", msg);

} catch (JSONException e) {

logger.error("產生JSON格式出錯" + e);

obj.put("flag", "0");

obj.put("msg", "由於網路問題,資料處理失敗");

} finally{

return obj.toString();

}

}

2.action中的使用案例

public ActionForwardadd(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response)throws IOException,AppException {

PrintWriter out = getPrintWriter(response);

String docTitle = Tool.getDefaultValue(request, "docTitle", "");

String uid = Tool.getDefaultValue(request, "uid", "");

try {

Long flag1 = WeiboWidgetDao.add(docTitle, uid);

Long flag2 = WeiboWidgetDao.addRel(docTitle, uid);

if (flag1 !=null && flag2 != null) {

responseStr(out, createJsonObject("1","添加成功!"));

WeiboWidgetDao.updateCache("doc", docTitle);// 更新詞條緩衝

} else {

responseStr(out, createJsonObject("0","添加詞條微博失敗!"));

}

} catch (Exception e) {

logger.debug("添加詞條微博失敗:" + e);

responseStr(out,createJsonObject("0","添加詞條微博失敗,發生異常!"));

}

return null;

}

3.js中的使用案例

儲存

function save() {

// 儲存前驗證

var docTitle = $("#win_docTitle").val();

var uid = $("#win_uid").val();

if (docTitle !="" && uid != "") {

$.ajax({

dataType: 'json',

type: 'POST',

url: '/weiboWidget.do',

data: {

'action' : $("#win_action").val(),

'id' : $("#win_id").val(),

'docTitle' : $("#win_docTitle").val(),

'uid' : $("#win_uid").val(),

},

success : function(data) {

if (data.flag == 1) {

alert(data.msg);

window.location.href= "/weiboWidget.do?action=show";

}else {

alert(data.msg);

}

returnfalse;

},

error: function() {

alert("由於網路問題,儲存資料失敗!");

returnfalse;

}

});

}else {

alert("詞條名和微博UID不可為空!");

return false;

}

}

刪除

/**

* 大量刪除操作

*

*/

function deleteData() {

var idList =document.getElementsByName("id_list");

var ids ="";

var docTitles ="";

// 檢查是否選擇內容

for (i = 0; i < idList.length;i++) {

if(idList[i].checked) {

var temp =idList[i].value.split(";");

ids+= temp[0] + ",";

docTitles+= temp[1] + ",";

}

}

if (ids =="") {

alert("請選擇紀錄!");

}else {

$.post("/weiboWidget.do?action=delete&" +new Date(), {

'ids' : ids,

'docTitles' : docTitles

},function(data) {

window.location.href= "/weiboWidget.do?action=show";

returnfalse;

});

}

}


轉載請指明:http://blog.csdn.net/yangkai_hudong

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.