我需要显示一个网格,后端有 Struts2。我有一个Tuple
代表数据的类,我在其中创建了一个toJson()
要返回的方法JsonObject
(使用 Google GSON 框架)
在一天结束时,我得到了空网格,如图所示。
以下是详细信息:
public class Tuple implements Serializable{
//other methods here..
public JsonObject toJson() {
//create JSON referemce
jSonRef = new JsonObject();
jSonRef.add("ProposalId", new JsonPrimitive( m_lngProposalId) );
jSonRef.add("ProposalLabel", new JsonPrimitive(m_strProposalLabel) );
jSonRef.add("AnalysisStatus", new JsonPrimitive(m_strAnalysisStatus) );
jSonRef.add("lockedBy", m_strLockedBy!=null? new
JsonPrimitive(m_strLockedBy): new JsonPrimitive("") );
return jSonRef ;
}
// other stuff here ...
}//end of the class
jSonRef
是类的成员变量。
struts.xml
是这样的:
<action name="JsonGetter" class="com.bvn.ecost.framework.actions.JSonGetterAction">
<result name="success" type="json"/>
</action>
动作类是:
package com.bvn.ecost.framework.actions;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.google.gson.JsonArray;
import com.opensymphony.xwork2.Action;
public class JSonGetterAction extends SuperAction{ //extends SuperAction
private static final Logger log = LogManager.getLogger(ReportDealAction.class);
private String results = null ;
public String execute() {
JsonArray propsArray = loadProposalsJson() //method from superclass, returning
//JsonArray
results = propsArray.toString() ;
return Action.SUCCESS ;
}
public String getResults() {
return results;
}
public void setResults(String results) {
this.results = results;
}
}
JSP 页面:
<code>
<%@ page trimDirectiveWhitespaces="true" %>
<%@ page language="java" %>
<%@ page import="com.bvn.ecost.*" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Connection" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
try {
%>`
<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns="http://www.w3.org/1999/xhtml" >
<HEAD>
<link rel="stylesheet" type="text/css" href="css/mainStyles.css" />
<link type="text/css" href="css/redmond/jquery-ui-1.8.23.custom.css" media="screen"
rel="stylesheet" />
<link type="text/css" href="css/ui.jqgrid.css" media="screen" rel="stylesheet" />
<link type="text/css" href="css/jquery.loadmask.css" media="screen" rel="stylesheet" />
<link type="text/css" href="css/ui.app.css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.8.23.custom.min.js"></script>
<script type="text/javascript" src="js/grid.locale-en.js"></script>
<script type="text/javascript" src="js/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.js"></script>
<script type="text/javascript" src="js/jquery.loadmask.js"></script>
<script type="text/javascript" src="js/app-helper.js"></script>
<script type="text/javascript">
$(document).ready(function (){
$.getJSON('JsonGetter.action', function(data) {
alert("Inside .getJson()") ;
console.log("data->results: "+data.results) ;
var this_grid;
this_grid = $this_app.define_grid("#grid-results", {
caption: 'Proposal Grid',
pager: '#pager-results',
data: $.extend(true, [], data.results),
datatype: "local",
colModel: [
{ name: 'ProposalId', index: 'ProposalId', width: 120, label: 'Proposal Id' },
{ name: 'ProposalLabel', index: 'ProposalLabel', width: 120, label: 'Label' },
{ name: 'AnalysisStatus', index: 'AnalysisStatus', width: 120, label: 'Status' },
{ name: 'lockedBy', index: 'lockedBy', width: 440, label: 'Locked by' }
],
inline_editing: false,
pager_options: {
del: false
});
}
) <!-- end of getJSon() -->
});
</script>
</HEAD>
<BODY>
<table align="center" width="100%"> <!-- Wrapper table -->
<tr>
<td>
<table align="center" width="100%"> <!-- Header image table -->
<jsp:include page="commonHead.jsp" flush="true" />
<tr>
<td colspan="10" align="center" style="color:#00509e">
<b><s:property value="statusString"/></b>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table align="center" width="60%">
<tr align="center">
<TD align="left">
<div id="div-results">
<table id="grid-results">
</table>
<div id="pager-results">
<div/>
</div>
<span id="results-status-message"></span>
</TD>
</tr>
</table>
</td>
</tr>
</table>
</BODY>
</HTML>
<%
}
catch (Exception e) {
Helper.handleException(e, request, response, out);
}
%>
我得到完整的页面,但空网格:
"console.log("data->results: "+data.results) ;" 这一行 在浏览器 JavaScript 控制台中显示 Json,如下所示:
data->results: [{"ProposalId":35145,"ProposalLabel":"US Laminating Corp 1","AnalysisStatus":"WIP","lockedBy":"cost-analyzer-admin"}]
我知道这可能是不正确的,但不知道如何。另外,我尝试在操作中将结果变量作为 JsonObject (并将其获取值的方式更改为此)
results.add("result", propsArray) ;
然后通过指定 data: 更改 JSP 行,如下所示:
data: $.extend(true, [], data.results.result),
UnsupportedOperationException
但是我在浏览器中得到了一些东西。