使用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> <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]
不明白原因是什么?
附件是源码,哪位有空劳烦给看一下