当前位置: 首页 > 知识库问答 >
问题:

需要jqgrid和struts2的完整示例

燕经国
2023-03-14

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

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

支柱。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>

网状物xml

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

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd“id=“WebApp\u id”版本=“3.0”

<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 */

        }

用户ction.java

package com.developer.am.action;

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

指数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>

共有1个答案

孟茂学
2023-03-14

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

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

 "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"
    }
$("#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"
  )};

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

 类似资料:
  • 问题内容: 我在搞弄JAX- RS,制作了一个应用程序,该应用程序调用产生JSON的REST服务。我尝试了Jersey,一切都很好,但是由于我的应用程序需要使用JDK5进行构建,因此我不得不切换到RESTEasy。我将web.xml更改为以下内容: 因此,我希望每个以/ rest开头的URL都将由RESTEasy处理。我的服务如下: 使用Jersey可以正常工作,http:// localhost

  • TS 配置文件路径在/etc/trafficserver/,分功能配置不同配置文件,常用的配置文件如下: records.config 主功能参数配置文件 remap.config 业务域名规则配置文件 storage.config Cache 存储配置文件 squid 配置转TS配置参考 配置示例 Storage.config ATS 支持目录配置,也支持裸盘配置,无需文件系统支持 目录配置方式

  • 我真的需要帮助。 我试图在新机器上运行我的jsp应用程序,jetty给了我这个错误:org.apache.jasper.JasperException:pwc6345:调用javac时有一个错误。需要完整的JDK(而不仅仅是JRE 编辑:问题解决-这是月食月星错误-在月食开普勒,它正在工作

  • 我想知道在不运行require的情况下,节点的require将解析到什么路径。 例如 在内部node.js将查看它的PATH,弄清楚它是一个目录、一个js文件、一个json文件等。,然后加载文件。 有没有办法在不实际加载模块的情况下只处理完整解析的路径? 我正在寻找的输出是这样的 重要的部分是. js扩展名,因为节点必须完全解析路径。 注意:无法解决此问题。如果some_module实际上是一个包

  • 以下完整长度示例指定YAML的构造,其中包括符号和各种表示,这些表示在以JSON格式转换或处理它们时将非常有用。 这些属性在JSON文档中也称为键名。 这些符号是出于安全目的而创建的。 上述YAML格式表示具有各种其他属性的默认值,适配器和主机的各种属性。 YAML还会记录生成的每个文件,这些文件会保留生成的错误消息的跟踪。 在以JSON格式转换指定的YAML文件时,得到所需的输出,如下所述 -