基于上一篇日志的实例的基础上修改:

CRUDService.java:

package client;

import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.core.client.GWT;

public interface CRUDService extends RemoteService {
     Student[] getStudent(String PageNum)     ;

    String getPagenum();

    public static class App {
        private static CRUDServiceAsync ourInstance = null;

        public static synchronized CRUDServiceAsync getInstance() {
            if (ourInstance == null) {
                ourInstance = (CRUDServiceAsync) GWT.create(CRUDService.class);
                ((ServiceDefTarget) ourInstance).setServiceEntryPoint(GWT.getModuleBaseURL() + "CRUD/CRUDService");
            }
            return ourInstance;
        }
    }
}


CRUDServiceImpl.java:

package server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import client.CRUDService;
import client.Student;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.cfg.Configuration;
import java.util.List;
import java.util.Iterator;

public class CRUDServiceImpl extends RemoteServiceServlet  implements CRUDService {
    private static final SessionFactory sessionFactory;
    String Pagenum = "1";
    static {
            try {
                // Create the SessionFactory from hibernate.cfg.xml
                sessionFactory = new Configuration().configure().buildSessionFactory();
            } catch (Throwable ex) {
                // Make sure you log the exception, as it might be swallowed
                System.err.println("Initial SessionFactory creation failed." + ex);
                throw new ExceptionInInitializerError(ex);
            }
        }

        public static SessionFactory getSessionFactory() {
            return sessionFactory;
        }

    public List ListStudent(String PageNum){
       Session session =  getSessionFactory().getCurrentSession() ;
        session.beginTransaction();
         Query query = session.createSQLQuery("select * from t_student")
        .addScalar("id", Hibernate.LONG)
        .addScalar("name", Hibernate.STRING)
        .addScalar("email", Hibernate.STRING);
        int PageSize = 10;

        try{
            if (Integer.parseInt(PageNum)!=0 | PageNum!=null ){
            query.setFirstResult((Integer.parseInt(PageNum)-1) * PageSize);
            query.setMaxResults(PageSize);
            }else{
            query.setFirstResult(0);
            query.setMaxResults(PageSize);
            }
        }catch(Exception e){
            query.setFirstResult(0);
            query.setMaxResults(PageSize);
        }
        List ls = query.list();
        session.getTransaction().commit();
        return ls;
    }

    public int CountStudent(){
       Session session =  getSessionFactory().getCurrentSession() ;
        session.beginTransaction();
         List ls = session.createSQLQuery("select count(*) from t_student").list();
        session.getTransaction().commit();
        return Integer.parseInt(ls.iterator().next().toString());
    }

        public Student[] getStudent(String PageNum){
             this.setPagenum(PageNum);
             Student[] student = new Student[this.CountStudent()];
            int i = 0;
          for(Iterator it = this.ListStudent(PageNum).iterator();it.hasNext();i++) {
             Object[] ob = (Object[] )it.next();
                student[i]=new Student(ob[0].toString(),ob[1].toString(),ob[2].toString());
              
          }

             return student;
          }

     public void setPagenum(String pagenum){
         this.Pagenum = pagenum;
     }

    public String getPagenum() {
        return Pagenum;  //To change body of implemented methods use File | Settings | File Templates.
    }

}


CRUDServiceAsync.java :

package client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface CRUDServiceAsync {

    void getStudent(String PageNum, AsyncCallback async);

    void getPagenum(AsyncCallback async);
}

CRUD.java :

package client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class CRUD implements EntryPoint {
    private CRUDServiceAsync  crudServiceAsync ;
    VerticalPanel main = new VerticalPanel();
    FlexTable lb = new FlexTable();
    HorizontalPanel hp = new HorizontalPanel();
    Button nextpage = new Button("nextpage");
    Button prepage = new Button("prepage");
    private String pagenum = "1";

    public void setPagenum(String pagenum){
        this.pagenum=pagenum;
    }
    public String getPagenum(){
        return this.pagenum;
    }
    int  prepagenum = 1;
    int  nextpagenum =1;

    public void onModuleLoad() {
        main.add(lb);
        main.add(hp);
        hp.add(prepage);
        hp.add(nextpage);
        RootPanel.get().add(main);
        showstudentlist("1");

       prepage.addClickListener(new ClickListener(){
       public void onClick (Widget sender){
       prepagenum = Integer.parseInt(getPagenum())-1;
       showstudentlist(String.valueOf(prepagenum));
       }
     });

     nextpage.addClickListener(new ClickListener(){
      public void onClick (Widget sender){
       nextpagenum = Integer.parseInt(getPagenum())+1 ;
     showstudentlist(String.valueOf(nextpagenum));
      }
      });
 
    }

     private void  showstudentlist(String pagenum) {
         CRUDService.App.getInstance().getStudent(pagenum,new AsyncCallback(){
                  public void onFailure(Throwable caught) {
                  }
                  public void onSuccess(Object result) {
                    Student s[] = ( Student[])result ;
                      for (int i=0;i<=s.length;i++){
                          lb.setText(i,0,s[i].id);
                          lb.setText(i,1,s[i].name);
                          lb.setText(i,2,s[i].email);
                      }
                  }
              });

        CRUDService.App.getInstance().getPagenum(new AsyncCallback(){
            public void onFailure(Throwable caught) {}
            public void onSuccess(Object result) {
                   setPagenum((String)result);
            }
        });

    }

}


posted on 2008-11-08 15:25 lzj520 阅读(730) 评论(0)  编辑  收藏 所属分类: Ajax个人学习日记GWT

只有注册用户登录后才能发表评论。


网站导航: