egosword 2014-05-11 14:50
浏览 349
已采纳

关于dataTable 服务器端json字符串的传递

使用struts2自带的json result机制前台取不到值
[code="java"]
public class DataTablesAction {
private String DTJson;
public String toList(){
return "success";
}
public String list(){
try {
HashMap queryConditionMap = new HashMap();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
DataTablesParameter dtp =DtpUtils.getDTParameter(request);
ResultListBean rsBean = new ResultListBean();
rsBean.setStartRecord(dtp.getStartRecord());
rsBean.setDisplayRecord(dtp.getDisplayRecord());
DataDAO dao=new DataDAO();
rsBean=dao.findAll(rsBean, queryConditionMap, dtp);
DTJson = DtpUtils.toJsonForDTS(rsBean.getResultList(),rsBean.getTotalRecord(),Integer.parseInt(dtp.getSEcho()));

        return "success";
    } catch (Exception e) {
        e.printStackTrace();
        return "error";
    }
}
public String getDTJson() {
    return DTJson;
}
public void setDTJson(String dTJson) {
    DTJson = dTJson;
}

}

[/code]
[code="xml"]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">




/WEB-INF/jsp/welcome.jsp


/WEB-INF/jsp/menu.jsp


/WEB-INF/jsp/showDataTables.jsp



DTJson




[/code]
改为response.out就可以获取到
[code="java"]
public String list(){
try {
HashMap queryConditionMap = new HashMap();
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
DataTablesParameter dtp =DtpUtils.getDTParameter(request);
ResultListBean rsBean = new ResultListBean();
rsBean.setStartRecord(dtp.getStartRecord());
rsBean.setDisplayRecord(dtp.getDisplayRecord());
DataDAO dao=new DataDAO();
rsBean=dao.findAll(rsBean, queryConditionMap, dtp);
response.getWriter().print(DtpUtils.toJsonForDTS(rsBean.getResultList(),rsBean.getTotalRecord(),Integer.parseInt(dtp.getSEcho())));
return null;
} catch (Exception e) {
e.printStackTrace();
return "error";
}
}
[/code]
[code="xml"]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">




/WEB-INF/jsp/welcome.jsp


/WEB-INF/jsp/menu.jsp


/WEB-INF/jsp/showDataTables.jsp




[/code]
页面脚本:
[code="javascript"]
$(document).ready(function(){
TableTools.DEFAULTS.aButtons = [];
var dt=$("#dataList").dataTable({
//"sScrollY": "400px",
"bScrollCollapse": true,
"bPaginate": true,
"bJQueryUI": true,
"sPaginationType": "full_numbers",
"aLengthMenu": [[10, 25, 50, -1], [10, 25, 50, "全部"]],

"bLengthChange":true,

"oLanguage": {
"sUrl": "include/dataTables_cn.txt"
},
"bSort":true,
"aaSorting": [[ 1, "asc" ]],
"sDom": '<"H"Tfrl>t<"F"ip>',

"bProcessing": true,
"bServerSide": true,

"sAjaxSource": "getListForDataTables.action?rand="+Math.random(),
"fnServerData" : function(sSource, aoData, fnCallback) {
$.ajax({

"type" : "post",
"url" : sSource,

"dataType" : "json",
"data" : aoData,

"success" : function(resp) {
fnCallback(resp);
$("#selectAll").removeAttr("checked");

                }
            });
         },
         "aoColumns": [
              {"sName":"all","sClass":"center",
               "bSearchable": false,"bSortable":false,
                "fnRender":function(oObj){
                    return "<input type='checkbox' name='isCheck' value='"+oObj.aData[1]+"'/>";
                }
              },
              { "sName": "wpbh", "sClass": "center"},
              { "sName": "wpmc", "sClass": "center"},
              { "sName": "rkrq", "sClass": "center"},
              { "sName": "dj", "sClass": "center"},
              { "sName": "kcsl", "sClass": "center"},
              { "sName": "lyr", "sClass": "center"},
              { "sName": "operation", "sClass": "center",
                "bSearchable": false,
                "bSortable":false,
                "fnRender": function (oObj) {
                    return "<a href= 'javascript:;' onclick='modify(\""+oObj.aData[1]+"\")'><img src='images/edit.png'/>修改</a>&nbsp;<a href= 'javascript:;' onclick='deleteRow(\""+oObj.aData[1]+"\")'><img src='images/delete.png'/>删除</a>";
                }
              }
          ]
        });
        $("#selectAll").click(function(){
            if($(this).attr("checked")=="checked"){
                $("input[name='isCheck']").attr("checked","checked");
            }else{
                $("input[name='isCheck']").removeAttr("checked");
            }
        });
    });

[/code]
[code="html"]



















物品编号 物品名称 入库时间 单价(元) 库存数量 领用人 操作


[/code]
不明白原因是什么?
附件是源码,哪位有空劳烦给看一下
  • 写回答

2条回答 默认 最新

  • konghangzyj 2014-05-11 18:16
    关注

    可以参考result type=“json”的配置使用方法

    http://kingxss.iteye.com/blog/1622455

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路