我是使用Struts2的JQgrid的初学者。
我尝试过这段代码,但我没有网格中的记录,但是我的所有记录都在行动中。
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<%@ taglib prefix="sjg" uri="/struts-jquery-grid-tags"%>
......
.......
<s:url id="remoteurl" action="jsontable"/>
<sjg:grid
id="gridtable"
caption="Application Data"
dataType="json"
href="%{remoteurl}"
pager="true"
gridModel="gridModel"
rowList="10,15,20"
rowNum="15"
rownumbers="true"
>
<sjg:gridColumn name="state" index="state" title="State" sortable="true"/>
<sjg:gridColumn name="city" index="city" title="City" sortable="false"/>
<sjg:gridColumn name="district" index="district" title="District" sortable="false"/>
</sjg:grid>
package com.sttl.rpsc.action;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import com.sttl.rpsc.action.base.BaseAction;
import com.sttl.rpsc.dataobject.Application;
import com.sttl.rpsc.util.HibernateUtil;
public class testAction extends BaseAction {
private List<Application> gridModel;
private List<Application> myCustomers;
private Integer rows = 0;
private Integer page = 0;
private Integer total = 0;
private Integer record = 0;
private String sord;
private String sidx;
private String searchField;
private String searchString;
private String searchOper;
private boolean loadonce = false;
public List<Application> getGridModel() {
return gridModel;
}
public void setGridModel(List<Application> gridModel) {
this.gridModel = gridModel;
}
public List<Application> getMyCustomers() {
return myCustomers;
}
public void setMyCustomers(List<Application> myCustomers) {
this.myCustomers = myCustomers;
}
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 record;
}
public void setRecord(Integer record) {
this.record = record;
}
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 String getSearchField() {
return searchField;
}
public void setSearchField(String searchField) {
this.searchField = searchField;
}
public String getSearchString() {
return searchString;
}
public void setSearchString(String searchString) {
this.searchString = searchString;
}
public String getSearchOper() {
return searchOper;
}
public void setSearchOper(String searchOper) {
this.searchOper = searchOper;
}
public boolean isLoadonce() {
return loadonce;
}
public void setLoadonce(boolean loadonce) {
this.loadonce = loadonce;
}
public String getJSON()
{
return getAllApplicationData();
}
public String getAllApplicationData(){
String result = SUCCESS;
Query q = null;
try{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
q = session.createQuery("from Application");
this.setGridModel(q.list());
if(this.getGridModel()!=null)
System.out.println("gridModel.Size() : "+this.getGridModel().size());
else
System.out.println("gridModel null");
int to = (rows * page);
int from = to - rows;
//Count Rows (select count(*) from custumer)
record = gridModel.size();
System.out.println("record : "+record);
//Your logic to search and select the required data.
//gridModel = CustumerDAO.find(from, to);
//calculate the total pages for the query
total =(int) Math.ceil((double)record / (double)rows);
System.out.println("total : "+total);
}catch(Exception e){
e.printStackTrace();
}
return result;
}
}
<package name="default" extends="struts-default,json-default" namespace="/">
..........
...........
<action name="jsontable" class="com.sttl.rpsc.action.testAction" method="getJSON">
<interceptor-ref name="basicStack" />
<result name="success" type="tiles">/testgrid.tiles</result>
<result name="error" type="redirect">/testgrid.tiles</result>
<result name="cancel" type="redirect">/testgrid.tiles</result>
</action>
<definition name="/testgrid.tiles" extends="baseLayoutNew">
<put-attribute name="title" value="Test Grid" />
<put-attribute name="body" value="/jsp/examination/testGrid.jsp" />
</definition>
这里我得到了列表大小:183,当我迭代动作文件时,我得到了所有记录,但我无法进入网格状态。我该怎么办?那里没有错误。
在您的网格中,您希望重新调整JSON。因此,在动作映射中,您必须将结果类型更改为json
:
<action name="jsontable" class="com.sttl.rpsc.action.testAction" method="getJSON">
<result name="success" type="json"/>
</action>
json
结果类型由Struts2 JSON plugin添加到结果类型中。确保插件位于类路径中,并且您的包正在扩展json-default
。通过将json
指定为结果类型,您可以将操作序列化为JSON,使其可用于javascript代码(在您的情况下为网格)以读取数据。