【发布时间】: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 上试过。