1

バックエンドに Struts2 を配置して、グリッドを表示する必要があります。私は(Google GSONフレームワークを使用して)返すTupleメソッドを作成したデータを表すクラスを持っていますtoJson()JsonObject

一日の終わりに、写真に見られるように、空のグリッドが表示されます。

詳細は次のとおりです。

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しかし、私はブラウザで何らかのものを取得します。

4

2 に答える 2

0

私が抱えている問題はJquery/gqgird/javascriptなどを理解していないことが原因だと考えて、かなり長い間これに苦労していました. Google gson ライブラリを使用し、データにマップするカスタム Java Bean オブジェクトをシリアル化します。多分誰かがこれを便利だと思うかもしれません、そして多くの時間を費やす必要はありません:

私が得ていたエラーは、Struts json プラグインがリフレクションを使用して、シリアル化するオブジェクトのプロパティを見つけるという事実によるものでした。その結果、JsonObject は Google ライブラリから JsonElement を拡張するため、struts はシリアル化されると思われる getter メソッドの束を認識します。すべてのメソッド

getAsBigDecimal()
getAsString(..)
etc..

プロパティとして解釈されました

asBigDecimal
asString
asBoolean
etc..

Jsonオブジェクトで見つかると予想されていました。もちろん、それらは私のカスタム データには見つかりませんでした。もともとは JsonObject に追加されていました。

addProperty()

struts-json-plugin に深く埋もれているため、 InvocationTargetException に関連する JsonException がキャッチされました。

助けてくれた人に感謝します。

于 2013-07-25T20:45:56.120 に答える