1. 程式人生 > >java後臺用的輸出流。怎麼在前臺用js怎麼接收

java後臺用的輸出流。怎麼在前臺用js怎麼接收

後臺Java程式碼:

     @RequestMapping(value = "/friendLinks/friendLinksList.do")
    public void getfriendLinksList(HttpServletRequest request,
            HttpServletResponse response, ModelMap modelMap){

        response.setCharacterEncoding("UTF-8");
        String pageNoStr = request.getParameter("pageNo");
        Integer pageNo = 0;

        try {
            pageNo = Integer.valueOf(pageNoStr);
        } catch (Exception e) {
            pageNo = 0;
        }
        Page page = new Page();
        page.setPageNo(pageNo);

        //按分頁條件查詢使用者
        List<FriendLinks> linksList = null;
        try {
            linksList = friendLinksService.getFriendLinksList(page);
            //將查出來的友情連結和分頁資訊放到json字串中
            JSONArray jsonList = new JSONArray();
            JSONObject bj = JSONObject.fromObject(page);
            jsonList.add(bj);
            for (FriendLinks links : linksList) {
                JSONObject obj = JSONObject.fromObject(links);
                jsonList.add(obj);
            }
            response.getWriter().println(jsonList.toString());
            response.getWriter().flush();
            response.getWriter().close();
        } catch (Exception e) {

            e.printStackTrace();
        }
        return ;
    }

JS中程式碼:

     function fillData(pageNo) {
    if (pageNo == "") {
        pageNo = thisPageNo;
    }

    var stateValue = $("#stateValue").val();
    var titleValue = $("#titleValue").val();

    $.ajax({
            url:contextPath + "/friendLinks/friendLinksList.do", 
            type:"POST",
            data:{
                  pageNo:pageNo,
                  stateValue:stateValue,
                  titleValue:titleValue
                 }, 
            dataType:"json", 
            beforeSend:function () {
            $("#pageTd a").remove();
    }, success:function (data) {
            var page = data[0];
            if (page.count == 0) {
                var noDataMsg = "<tr><td colspan=\"9\" class=\"xiamian\">無相關資料</td></tr>";
                $("#dataBody").html(noDataMsg);
                var pageMsg = "總記錄數:<strong>0</strong>"+
                              "&nbsp;&nbsp;當前頁:<strong>0/0</strong>&nbsp;&nbsp;"+
                              "<a href='javascript:;'>首頁</a>&nbsp;"+
                              "<a href='javascript:;'>上一頁</a>&nbsp;"+
                              "<a href='javascript:;'>下一頁</a>&nbsp;"+
                              "<a href='javascript:;'>尾頁</a>&nbsp;";
                              $("#pageButtonBefore").html(pageMsg);
            } else {
                var dataMsg = "";
                var caozuo = "";
                var useOrNot = "";
                for (var i = 1; i < data.length; i++) {
                    caozuo = "";
                    caozuo = caozuo + "<a href='javascript:;' onclick='checkJump(\""+data[i].id+"\")'>檢視</a>";
                    caozuo = caozuo + "<a href='javascript:;' onclick='editJump(\""+data[i].id+"\")'>修改</a>";
                    caozuo = caozuo + "<a href='javascript:;' onclick='deleteJump(\""+data[i].id+"\")'>刪除</a>";


                    if(data[i].status == "0"){

                        status = "是";
                    }else{
                        status = "否";
                    }
                    dataMsg = dataMsg + "<tr><td>" + i + 
                                        "</td><td>" + data[i].linkName + 
                                        "</td><td>" + data[i].linkSite + 
                                        "</td><td>" + status + "</td><td>"; 
                    dataMsg = dataMsg + caozuo;                 
                    dataMsg = dataMsg + "</td></tr>";
                }
                $("#dataBody").html(dataMsg);

                var pageMsg = "總記錄數:<strong>"+page.count+"</strong>&nbsp;&nbsp;當前頁:<strong>"+page.pageNo+"/"+page.totalPage+"</strong>&nbsp;&nbsp;"+
                              "<a href='javascript:fillData(\"1\");'>首頁</a>&nbsp;&nbsp;"+
                              "<a href=javascript:fillData(\"" + page.pre + "\");>上一頁</a>&nbsp;&nbsp;"+
                              "<a href=javascript:fillData(\"" + page.next + "\");>下一頁</a>&nbsp;&nbsp;"+
                              "<a href=javascript:fillData(\"" + page.totalPage + "\");>尾頁</a>&nbsp;&nbsp;";
                $("#pageButtonBefore").html(pageMsg);
            }
    }, error:function () {
          alert("獲取相關資料失敗!");
    }});

}其實能看出來,JS怎麼接收值跟後臺用什麼方式傳沒有多大關係,關鍵是看傳過來的值格式是啥就好了。
傳回來的是字串,那就直接按照字串處理,傳回來的是Json格式的那就直接按照解析Json來處理。