【问题标题】:Struts2-Jquery Grid : Data not getting displayedStruts2-Jquery Grid:数据未显示
【发布时间】:2026-02-14 10:10:02
【问题描述】:

我正在使用 struts2-jquery 网格。

我的动作类中的gridmodel 正在正确更新。但是,网格上没有显示任何内容。

struts.xml

<struts>

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

        <action name="listUsers" class="com.pilot.web.action.ListUsers" method="getJSON">
            <result name="success">/jsp/userList.jsp</result>
            <result name="input">/jsp/userList.jsp</result>
        </action>
</package> 

userList.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<sj:head jqueryui="true" jquerytheme="redmond"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<h3>Application Users</h3>
<s:actionerror/>
<s:actionmessage/>
<s:url id="remoteurl" action="/searchAndUpdate/listUsers"/>
    <sjg:grid
        id="gridtable" 
        caption="Application Users" 
        dataType="json" 
        href="%{remoteurl}" 
        pager="true" 
        gridModel="gridModel" 
        rowList="10,15,20" 
        rowNum="15" 
        rownumbers="true" 
        resizable="true" 
        viewrecords="true" 
    >
        <sjg:gridColumn name="id" index="id" title="Id" formatter="integer" sortable="false"/>
        <sjg:gridColumn name="userName" index="userName" title="User Name" sortable="true"/>
        <sjg:gridColumn name="fullName" index="fullName" title="Full Name" sortable="false"/>
        <sjg:gridColumn name="email" index="email" title="EMail" sortable="false"/>
    </sjg:grid>
</body>
</html>

ListUsers.java

package com.pilot.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionSupport;
import com.oxxo.pilot.backend.jdbc.dao.impl.User;


public class ListUsers extends ActionSupport {
        private static final long serialVersionUID = 1L;
        private List<User> gridModel;
        //get how many rows we want to have into the grid - rowNum attribute in the grid
        private Integer rows = 0;
        //Get the requested page. By default grid sets this to 1.
        private Integer page = 0;
        // Your Total Pages
        private Integer total = 0;
        // All Records
        private Integer records = 0;
        // sorting order - asc or desc
        private String sord;
        // get index row - i.e. user click to sort.
        private String sidx;
        public String getAllApplicationData() {
                List<User> users = userListFromDb();
                if (users != null && getSord() != null
                                && getSord().equalsIgnoreCase("asc")) {
                        Collections.sort(users, null);
                }
                if (getSord() != null && "desc".equalsIgnoreCase(getSord())) {
                        Collections.sort(users, null);
                        Collections.reverse(users);
                }
                setRecord(users.size());
                int to = (getRows() * getPage());
                if (to > getRecord())
                        to = getRecord();

                setGridModel(users);
                setTotal((int) Math.ceil((double) getRecord() / (double) getRows()));
                System.out.println("Total is : " + getTotal());
                System.out.println("Record is : " + getRecord());
                for(User user: users){
                        System.out.println(user.getUserName() + ", " + user.getFullName() + ", " + user.getEmail());
                }
                if (hasActionMessages() || hasActionErrors()) {
                        return INPUT;
                }
                return SUCCESS;
        }

        private List<User> userListFromDb() {
           // MOCKED List for now
                List<User> users = new ArrayList<User>();
                User user1 = new User();
                user1.setId(1);
                user1.setUserName("user1");
                user1.setFullName("User ABC");
                user1.setEmail("user1@email.com");

                User user2 = new User();
                user2.setId(1);
                user2.setUserName("user2");
                user2.setFullName("User DEF");
                user2.setEmail("user2@email.com");

                users.add(user1);
                users.add(user2);
                return users; // TODO FETCH LIST OF USERS FROM DB
        }

        public String getJSON() {
                System.out.println("here in getJSON");
                return getAllApplicationData();
        }

        public Integer getRows() {
                return rows;
        }

        public void setRows(Integer rows) {
                this.rows = rows;
        }

        public Integer getPage() {
                return page;
        }

        public void setPage(Integer page) {
                this.page = page;
        }

        public Integer getTotal() {
                return total;
        }

        public void setTotal(Integer total) {
                this.total = total;
        }

        public Integer getRecord() {
                return records;
        }

        public void setRecord(Integer records) {
                this.records = records;
                if (this.records > 0 && this.rows > 0) {
                        this.total = (int) Math.ceil((double) this.records
                                        / (double) this.rows);
                } else {
                        this.total = 0;
                }
        }
        public List<User> getGridModel() {
                return gridModel;
        }
        public void setGridModel(List<User> gridModel) {
                this.gridModel = gridModel;
        }
        public String getSord() {
                return sord;
        }
        public void setSord(String sord) {
                this.sord = sord;
        }
        public String getSidx() {
                return sidx;
        }
        public void setSidx(String sidx) {
                this.sidx = sidx;
        }
        public void setSearchField(String searchField) {
        }

        public void setSearchString(String searchString) {
        }

        public void setSearchOper(String searchOper) {
        }

        public void setLoadonce(boolean loadonce) {
        }
        public void setSession(Map<String, Object> session) {
        }
}

我确实参考了struts2 jquery grid data not load?上的解决方案

但该解决方案不起作用。它没有在网格上显示数据,而是提示保存文件并且该文件包含以下信息:

{"JSON":"success","gridModel":[{"email":"user1@email.com","fullName":"用户 ABC","id":1,"userName":" user1"},{"email":"user2@email.com","fullName":"User DEF","id":1,"userName":"user2"}],"page":0,"record ":2,"rows":0,"sidx":null,"sord":null,"total":2147483647}*

据我了解,行和页面由插件自动处理。但是根据上面的 json 数据,它们并没有增加。将它们硬编码为gridmodel的大小,我得到的json数据是:

{"JSON":"success","allApplicationData":"success","gridModel":[{"email":"user1@email.com","fullName":"用户 ABC", "id":1,"userName":"user1"},{"email":"user2@email.com","fullName":"用户 DEF","id":1,"userName":"user2" }],"page":1,"record":2,"rows":2,"sidx":null,"sord":null,"total":1}

关于将 struts.xml 更新为

 <result-types>
        <result-type name="json" class="org.apache.struts2.json.JSONResult" />
        </result-types>
        <action name="listUsers" class="com.oxxo.pilot.web.action.ListUsers">
            <result name="success" type="json">/jsp/userList.jsp</result>
            <result name="input" type="json">/jsp/userList.jsp</result>
        </action>

json 数据显示在浏览器上。

谢谢, 希哈

【问题讨论】:

  • 您是否有任何插件可以在您的浏览器上自动下载特定数据格式?你试过不同的浏览器吗?
  • 不,我没有使用任何这样的插件。我在 IE 和 Chrome 上试过。

标签: jquery struts2


【解决方案1】:

正确的解决办法是:

Struts.xml:

<result-types>
            <result-type name="json" class="org.apache.struts2.json.JSONResult" />
        </result-types>

<action name="listUsers" method="getAllApplicationData"   class="com.pilot.web.action.ListUsers">
            <result name="success">/jsp/userList.jsp</result>
            <result name="input">/jsp/fail.jsp</result>
        </action>
        <action name="getData" method="getAllApplicationData"   class="com.pilot.web.action.ListUsers">
            <result name="success" type="json"></result>
        </action>

Index.jsp

<s:form action="/searchAndUpdate/listUsers" method="post">
        <s:submit name="ListUsers" value="List All Users" align="right" />
    </s:form>

userList.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<sj:head jqueryui="true" jquerytheme="redmond"/>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Style-Type" content="text/css" />
</head>
<body>
<h3>Application Users</h3>
<s:actionerror/>
<s:actionmessage/>
<s:url id="remoteurl" action="/searchAndUpdate/getData"/>
    <sjg:grid
        id="gridtable" 
        caption="Application Users" 
        href="%{remoteurl}" 
        pager="true" 
        gridModel="gridModel" 
        rowList="10,15,20" 
        rowNum="15" 
        rownumbers="true" 
        resizable="true" 
        viewrecords="true" 
        name="gridModel" 
    >
        <sjg:gridColumn name="id" index="id" title="Id" formatter="integer" sortable="false"/>
        <sjg:gridColumn name="userName" index="userName" title="User Name" sortable="true"/>
        <sjg:gridColumn name="fullName" index="fullName" title="Full Name" sortable="false"/>
        <sjg:gridColumn name="email" index="email" title="EMail" sortable="false"/>
    </sjg:grid>
</body>
</html>

这是一个有效的代码。工作得很好:)

问题是: jsp 接受数据类型 json 作为输入,并且 ListUsers 必须呈现一个 jsp。所以,我做了两个不同的动作。一个返回“json”,作为 jsp 的输入,一个在成功时呈现 jsp。

【讨论】:

    【解决方案2】:

    动作应该返回 JSON。

     <action name="listUsers" class="com.pilot.web.action.ListUsers" method="getJSON">
                <result name="success" type="json"/>
            </action>
    

    You can find a few grid examples here.

    【讨论】:

    • 我在写的时候确实尝试过。但它在浏览器而不是网格上显示 json 数据。可能是我也错过了一些我无法弄清楚的东西。
    • 你试过这个:&lt;result name="success" type="json"&gt;/jsp/userList.jsp&lt;/result&gt;,我说的是:&lt;result name="success" type="json"/&gt;。此外,您是否尝试过其他一些示例。
    • 是的,我也厌倦了这个。但这并没有解决问题。我确实检查了您提供的链接,但无法弄清楚我的代码中有什么问题/缺失。
    • 我现在只能说可能是插件和struts2的版本不匹配。尝试使用 struts2.2.3 和插件版本 3.2.1
    【解决方案3】:

    尝试在您的操作类中使用 2 个返回参数的方法。如果结果名称为 INPUT,则应返回正常参数。当您查询网格值时,您应该返回 JSON。我的意思是将您的操作结果更改为:

    <action name="listUsers_*" method={1} class="com.oxxo.pilot.web.action.ListUsers">
        <result name="success" type="json">/jsp/userList.jsp</result>
        <result name="input">/jsp/userList.jsp</result>
    </action>
    

    在你的行动课中

    public String execute() throws Exception { return INPUT;}
    public String getJSON() { return getAllApplicationData();}
    

    不知道execute()方法未实现时是否正常返回INPUT

    在您的页面中,添加 getJSON 以在您的操作类上调用 getJSON 方法。

    <s:url id="remoteurl" action="listUsers_getJSON"/>
    

    希望这有帮助,如果你解决了,请告诉我

    【讨论】:

    • 在动作支持类中它被定义为只返回SUCCESS
    • 你的意思是,如果它没有被覆盖?
    • 我猜,您使用的插件向浏览器发送了错误的 MIME 类型。你用的是哪个版本的 struts-jqgrid、struts-jquery 插件?你有额外的与你的网格相关的 javascripts 吗?
    • 我使用的是 3.2.1 版本。不,我没有使用任何 javascripts。