使用hibernate在struts2 jquery网格中不显示数据

时间:2014-01-30 04:54:05

标签: jquery hibernate struts2 grid

我是struts2中的初学者jquery grid.Grid显示在浏览器上但是没有填充网格。主要问题是它不在MyActionTable.java中。请帮帮我。

Customer.java

 package example;
 public class Customer  implements java.io.Serializable {


 private Integer id;
 private String name;

public Customer() {
}

public Customer(String name) {
   this.name = name;
}

public Customer(Integer id, String name) {
    this.id = id;
    this.name = name;
}

public Integer getId() {
    return this.id;
}

public void setId(Integer id) {
    this.id = id;
}
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}}

MyActionTable.java

 package example;
 import com.opensymphony.xwork2.ActionSupport;
 import database_util.MyFilterHib;
 import java.util.*;
 import org.hibernate.*;
 import org.hibernate.cfg.Configuration;

 public class MyActionTable extends ActionSupport{

public MyActionTable() {
    System.out.println("ActionTable********************");
}

private List<Customer>      gridModel;
private List<Customer>      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<Customer> getGridModel() {
    System.out.println("31****************");
    return gridModel;
}
public void setGridModel(List<Customer> gridModel) {
    this.gridModel = gridModel;
}
public List<Customer> getMyCustomers() {
    return myCustomers;
}
public void setMyCustomers(List<Customer> 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{
        SessionFactory sessionfactory=new      Configuration().configure().buildSessionFactory();
        SessionFactory sf=MyFilterHib.getSessionFactory();
        Session session;
        session = sessionfactory.getCurrentSession();
        session.beginTransaction();
        q = session.createQuery("from Customer");
        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;
}}

的helloWorld.jsp

<%@page contentType="text/html" pageEncoding="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 HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <sj:head jqueryui="true" jquerytheme="redmond"></sj:head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Grid Demo</title>
</head>
<body>
    <s:url  id="remoteurl" action="ntable"/>

    <sjg:grid
        id="gridtable"
        caption="Customer Examples"
        dataType="json"
        href="%{remoteurl}"
        pager="true"
        gridModel="gridModel"
        rowList="10,15,20"
        rowNum="15"
        rownumbers="true"
    >
        <sjg:gridColumn name="id" index="id" title="ID" formatter="integer" sortable="false" key="true"/>
        <sjg:gridColumn name="name" index="name" title="Name" editable="true"/>
    </sjg:grid>

</body>
</html>

struts.xml中

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>

<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="example" extends="struts-default,json-default" namespace="/">

    <action name="ntable" class="example.MyActionTable" method="getJSON">
        <result name="success" type="json"/>
    </action>
</package>
</struts>

0 个答案:

没有答案