DWR(AJAX的一種Java實現)做的分頁!!

zengbo0710發表於2007-05-15

DWR(AJAX的一種Java實現)做的分頁!!

1.以下是dwr的配置

(1)在web.xml中配置DWRServlet如下:

<servlet>
         <servlet-name>dwr-invoker</servlet-name>
         <display-name>DWR Servlet</display-name>
         <description>Direct Web Remoter Servlet</description>
         <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
         <init-param>
             <param-name>debug</param-name>
             <param-value>true</param-value>
         </init-param>
     </servlet>

     <servlet-mapping>
         <servlet-name>dwr-invoker</servlet-name>
         <url-pattern>/dwr/*</url-pattern>
     </servlet-mapping>

(2)在dwr.xml中配置

<dwr>
<allow>
   <create javascript="GetPages" creator="new">
    <param name="class" value="com.GetPages"></param>
   </create>
   <convert match="com.PageBean" converter="bean">
    <param name="include" value="job_id,job_desc,min_lvl,max_lvl"></param>
   </convert>
</allow>
</dwr>

2.編寫PageBean以存放要顯示的資料

private String job_id;
private String job_desc;
private String min_lvl;
private String max_lvl;

3.編寫getPage類實現分頁,程式碼如下:

public int getFirst(int page)
{
   pages=page;
   return pages;
}
public int getNext(int page)
{
   if(page==pageCount)
   {
    return pageCount;
   }else
   {
    pages=page;
    return pages+1;
   }
}
public int getUp(int page)
{
   if(page==1)
   {
    return pages;
   }else
   {
    pages=page;
    return pages-1;
   }
}
public int getLast(int page)
{
   return pageCount;
}
public List<PageBean> getJobs(int page)
{
   list=new ArrayList<PageBean>();
   String sql;
   con=connect.getCon();
   try {
    stm=con.createStatement();
    if(page==1)
    {
     sql="select top 5 * from jobs";
    }else
    {
     sql="select top 5 * from jobs where job_id not in (select top "+5*(page-1)+" job_id from jobs)";
    }
    rs=stm.executeQuery(sql);
    while(rs.next())
    {
     pb=new PageBean();
     pb.setJob_id(rs.getString("job_id").toString());
     pb.setJob_desc(rs.getString("job_desc").toString());
     pb.setMin_lvl(rs.getString("min_lvl").toString());
     pb.setMax_lvl(rs.getString("max_lvl").toString());
     list.add(pb);
    }
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   return list;
}

4.新建個頁面呼叫getPage類中的方法

<html>
   <head>
   <script type="text/javascript" src="dwr/util.js"></script>
   <script type="text/javascript" src="dwr/engine.js"></script>
   <script type="text/javascript" src="dwr/interface/GetPages.js"></script>
     <script type="text/javascript">
      var page=1;
    
      function first()
      {
       GetPages.getFirst(1,callback);
      }
      function callback(pages)
      {
       page=pages;
       GetPages.getJobs(pages,process);
      }
      function next()
      {
       GetPages.getNext(page,callback);
      }
      function up()
      {
       GetPages.getUp(page,callback);
      }
      function last()
      {
       GetPages.getLast(page,callback);
      }
      function process(item)
      {
       DWRUtil.removeAllRows("job");
        var cell=[
         function(item){return item.job_id},
         function(item){return item.job_desc},
         function(item){return item.min_lvl},
         function(item){return item.max_lvl}
        ];
        DWRUtil.addRows("job",item,cell);
      }
    
     </script>
   </head>
 
   <body onload="javaScript:first();">
   <table border="1">
    <tbody id="job">
   
    </tbody>
   </table>
     <a href="javaScript:first()">第一頁</a>&nbsp;&nbsp;
     <a href="javaScript:up()">上一頁</a>&nbsp;&nbsp;
     <a href="javaScript:next()">下一頁</a>&nbsp;&nbsp;
     <a href="javaScript:last()">最後一頁</a>&nbsp;&nbsp;
   </body>
</html>
 

相關文章