【问题标题】:Need full example for jqgrid with struts2需要带有 struts2 的 jqgrid 的完整示例
【发布时间】:2016-05-11 11:59:33
【问题描述】:

我在 struts2 中使用 jqgrid 插件。我能够在浏览器中看到 JSON 数据,但不确定如何在网格中显示该数据(使用 grid.jsp)。

以下是我正在使用的文件:

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<constant name="struts.devMode" value="true" />

<package name="default" namespace="/" extends="json-default">

    <result-types>
        <result-type name="json" class="com.googlecode.jsonplugin.JSONResult" />
    </result-types>

    <action name="getJSONResult" class="com.developer.am.action.JSONDataAction">
        <result type="json" />
    </action>

    <action name="ListUsers" class="com.developer.am.action.ListUsersAction">
        <result name="SUCCESS">grid.jsp</result>
    </action>

</package>

web.xml

<?xml version="1.0" encoding="UTF-8"?>

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" 版本="3.0"> Struts2_Json 索引.jsp

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

</filter>

<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

JSONDataAction.java

package com.developer.am.action; 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.Action;

public class JSONDataAction{


int id = 1;
String firstName = "ASHOK";
String lastName = "BHOR";
String username = "ab";
String email="ab@.com";

        /*setter getter methos */

        }

ListUsersAction.java

package com.developer.am.action;

public class ListUsersAction {
     public String execute() {
            return "SUCCESS";
        }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@taglib prefix="st" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<st:form action="getJSONResult">

<h1>Hello</h1>
<st:submit value="ok"></st:submit>
</st:form>

</body>
</html>

grid.jsp

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" type="text/css" media="screen" href="../css/jquery-ui-1.10.2.custom.css" />
<link rel="stylesheet" type="text/css" media="screen" href="../css/ui.jqgrid.css" />
<script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
<script src="js/jquery-ui-1.10.2.custom.min.js" type="text/javascript"></script>
<script src="js/i18n/grid.locale-en.js" type="text/javascript"></script>
<script src="js/jquery.jqGrid.min.js" type="text/javascript"></script>
<title>List Users</title>
<script type="text/javascript"> 
jQuery().ready(function (){
    alert('Hi');
    jQuery("#list1").jqGrid({
           url:'Struts2_Json/getJSONResult.action',
        datatype: "json",
           colNames:['User Id','First Name', 'Last Name', 'Username','Email'],
           colModel:[
               {name:'id',index:'id', width:75},
               {name:'firstName',index:'firstName', width:90},
               {name:'lastName',index:'lastName', width:100},
               {name:'username',index:'username', width:80},
               {name:'email',index:'username', width:80}                   
           ],
           rowNum:10,
           autowidth: true,
           rowList:[10,20,30],
           pager: jQuery('#pager1'),
           sortname: 'id',
        viewrecords: true,
        sortorder: "desc",
        caption:"Users"
    }).navGrid('#pager1',{edit:false,add:false,del:false});    
});                
</script>
</head>
<body>
    <table id="list1"></table>
    hi
    <div id="pager1"></div>
</body>
</html>

【问题讨论】:

  • 这是我看到您想要更改的一件事:我相信colModel 数组中的index 值应该是整数,而不是字符串。还有一些可以帮助您调试的东西:确保 JSONDataAction.java 为您提供有效的 JSON,并按照您的 JSP 期望的方式构建。鉴于您在此处发布的代码,很难验证这一事实。
  • 嗨@DavidGorsline 你能在下面提出建议吗:stackoverflow.com/questions/37881679/…

标签: json struts2 jqgrid


【解决方案1】:

我知道它已经很晚了,但仍然发布答案。好像有人面临同样的问题,所以他可以在这里获得帮助。

最近遇到了同样的问题,我通过使用 free-jqGrid 的 jsonReader 属性解决了它。

我从后端得到的 Json 是

 "gridModel1": [{
        "description": null,
        "filterLang": "en",
        "filterName": "welcome1",
        "filterType": "Announcement",
        "id": {
            "customer": "DemoCust",
            "languageId": "en",
            "name": "welcome1",
            "poolName": "w",
            "customerId": "23",
            "type": "Prompt",
            "version": "default"
        },
        "key": "xxyzz123",
        "numId": 1,
        "usedForReport": false,
        "value": "http:\/\/15.213.57.179:8080\/mcm\/resources\/Djezzy\/Questionnaire\/Versions\/default\/DemoCust\/Prompt\/en\/welcome1.wav"
    }

在 jqGrid 中使用 jsonReader 属性

$("#gridMod").jqGrid({
            url:"<<StrutsBackendCall>>",     
            datatype:"json",
            jsonReader: {
                root:"gridModel1"
            },
            colModel: [
                { name: "numId",index:"numId", label:"NumId" },

                { name: "filterName",index:"filterName", hidden:true, label:"id.Name"},

                { name: "id.poolName",index:"id.poolName",label:"Prompt Name" },
                { name: "id.name",index:"id.name", label:".wav File Name" },

                { name: "id.customerid",index:"id.customerid", hidden:true, label: "Tenant" }, 
                .....

            ],             
            /* loadonce:false, */
            guiStyle: "bootstrap",
            iconSet: "glyph",   
            sortorder: "asc",
            idPrefix: "gridModel1_",
            onSelectRowTopics:"rowselect_main1",
            pager: true,                
            cmTemplate: { autoResizable: true },
            rowNum:10,
            viewrecords: true,
            caption: "The grid, which uses predefined formatters and templates"
  )};

现在我可以看到网格中的数据了。如果不清楚,请评论将尽力提供帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-11
    • 2011-11-14
    • 2011-08-19
    • 2016-10-31
    • 2011-05-06
    • 1970-01-01
    • 2011-10-08
    相关资源
    最近更新 更多