Struts2 jQuery网格数据未加载 [英] Struts2 jQuery grid data not loading

查看:92
本文介绍了Struts2 jQuery网格数据未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JQgrid和Struts2的初学者.

I'm a beginner with JQgrid with Struts2.

我尝试过使用此代码,但是网格中没有记录,但是所有记录都在运行.

I had try with this code but I will got no records in grid, but I got all record in action.

<%@ 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>

testAction.java

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;
    }



}

struts.xml

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

tiles.xml

<definition name="/testgrid.tiles" extends="baseLayoutNew">
    <put-attribute name="title" value="Test Grid" />
    <put-attribute name="body" value="/jsp/examination/testGrid.jsp" />
 </definition>

在这里,我得到了列表大小:183,当我在操作文件中进行迭代时,我得到了所有记录,但无法进入网格.我应该怎么办?没有错误显示在那里.

Here I got list size: 183 and when I iterate in action file I got all records but I can't get in grid. What should I do? There's no error showing there.

推荐答案

在您的网格中,您希望重新调整JSON.因此,在操作映射中,您必须将结果类型更改为json:

In your grid you expect JSON to be retuned. So in you action mapping you have to change the result type to json as:

<action name="jsontable" class="com.sttl.rpsc.action.testAction" method="getJSON">
    <result name="success" type="json"/>
</action>

json结果类型通过 Struts2 JSON插件.确保插件在您的类路径中,并且您的包正在扩展json-default.通过将json指定为结果类型,您可以将操作序列化为JSON,使其可用于javascript代码(在您的情况下为网格)读取数据.

json result type is added to your result types by Struts2 JSON plugin. make sure the plugin is in your classpath and your package is extending json-default. By specifying json as your result type you're serializing your action as a JSON making it available for the javascript code (in your case the grid) to read the data.

这篇关于Struts2 jQuery网格数据未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆