码迷,mamicode.com
首页 > Web开发 > 详细

action中json的应用

时间:2015-01-02 15:50:18      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:

      这篇文章重点介绍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);

                            }

                            return false;

                     },

                     error: function() {

                            alert("因为网络问题,保存数据失败!");

                            return false;

                     }

              });

       }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";

                     return false;

              });

       }

}


转载请指明:http://blog.csdn.net/yangkai_hudong

action中json的应用

标签:

原文地址:http://www.cnblogs.com/gcczhongduan/p/4198455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!