基于上一篇日志的實(shí)例的基礎(chǔ)上修改:

          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 閱讀(737) 評論(0)  編輯  收藏 所屬分類: Ajax個人學(xué)習(xí)日記GWT
          主站蜘蛛池模板: 望都县| 桃江县| 新绛县| 江华| 吴江市| 棋牌| 盐山县| 东阿县| 云安县| 云浮市| 永定县| 澳门| 西乡县| 密云县| 库尔勒市| 合江县| 京山县| 和龙市| 呼伦贝尔市| 日土县| 夏津县| 玛多县| 怀集县| 昔阳县| 龙游县| 龙海市| 柯坪县| 镇江市| 阳春市| 海晏县| 崇礼县| 原阳县| 和田县| 五家渠市| 仪征市| 集安市| 辛集市| 呼图壁县| 安西县| 库车县| 凌源市|