Use DWR to easily implement Ajax Paging

Source: Internet
Author: User

Bkjia.com documentThis tutorial uses DWR (a Java Implementation of AJAX) as the page!

 1. The following is the configuration of DWR.

(1) Configure DWRServlet in web. xml as follows:

Reference content is as follows:

<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) Configure in dwr. xml

Reference content is as follows:
<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. Compile PageBean to store the data to be displayed

Reference content is as follows:
 Private String job_id;
 Private String job_desc;
 Private String min_lvl;
 Private String max_lvl;

3. Compile the getPage class to implement paging. The Code is as follows:

Reference content is as follows:
 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 )";
  }
  Rs1_cmd.exe cuteQuery (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. Create a page and call the method in the getPage class.

Reference content is as follows:

<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 ()"> page 1 </a> & nbsp;
   <A href = "javaScript: up ()"> previous page </a> & nbsp;
   <A href = "javaScript: next ()"> next page </a> & nbsp;
   <A href = "javaScript: last ()"> last page </a> & nbsp;
 </Body>
</Html>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.