问题描述
使用struts2自带的json result机制前台取不到值public class DataTablesAction {private String DTJson;public String toList(){return "success";}public String list(){try {HashMap<String,String> queryConditionMap = new HashMap<String,String>();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;}}<?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"><struts><package name="main" extends="json-default"><default-action-ref name="index"/><action name="index"><result name="success">/WEB-INF/jsp/welcome.jsp</result></action><action name="getMenu"><result name="success">/WEB-INF/jsp/menu.jsp</result></action><action name="showDataTables" class="action.DataTablesAction" method="toList"><result name="success">/WEB-INF/jsp/showDataTables.jsp</result></action><action name="getListForDataTables" class="action.DataTablesAction" method="list"><result type="json"><param name="root">DTJson</param></result></action></package></struts> 改为response.out就可以获取到public String list(){try {HashMap<String,String> queryConditionMap = new HashMap<String,String>();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";}}<?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"><struts><package name="main" extends="json-default"><default-action-ref name="index"/><action name="index"><result name="success">/WEB-INF/jsp/welcome.jsp</result></action><action name="getMenu"><result name="success">/WEB-INF/jsp/menu.jsp</result></action><action name="showDataTables" class="action.DataTablesAction" method="toList"><result name="success">/WEB-INF/jsp/showDataTables.jsp</result></action><action name="getListForDataTables" class="action.DataTablesAction" method="list"></action></package></struts> 页面脚本:$(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");}});}); <body> <table id="dataList" width="100%" align="center" style="font-size:11px;"> <thead> <tr> <th align="center" width="1"><input id="selectAll" type="checkbox" /></th> <th align="center" width="15%">物品编号</th> <th align="center" width="15%">物品名称</th> <th align="center" width="15%">入库时间</th> <th align="center" width="15%">单价(元)</th> <th align="center" width="15%">库存数量</th> <th align="center" width="15%">领用人</th> <th align="center" width="15%">操作</th> </tr> </thead> <tbody id="dataBody"> </tbody> <tfoot> </tfoot> </table> </body>不明白原因是什么?附件是源码,哪位有空劳烦给看一下
解决方案
可以参考result type=“json”的配置使用方法http://kingxss.iteye.com/blog/1622455
解决方案二:
你应该返回DTJson ,不是返回success