国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
struts2 JSP 實(shí)現(xiàn)分頁(yè)顯示
邊查資料邊摸索,終于把struts2+ JSP分頁(yè)搞定了。

環(huán)境JDK1.6+mysql5.0+jboss4.0+struts 2.0.11

主要有三個(gè)東西show.jsp、ShowAction.java、PageInformation.java

還需完善的地方:如果沒(méi)有前一頁(yè)、后一頁(yè),直接把這個(gè)鏈接在頁(yè)面上屏蔽掉

 因?yàn)椴幌朐?/span>Action里面有太多的鏈接數(shù)據(jù)庫(kù)的代碼,所以另外搞了一個(gè)PageInformation類,來(lái)完成數(shù)據(jù)庫(kù)查詢工作。

具體代碼如下:

package com.ClockWise.ray.jsp;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.ArrayList;

/*

 * 此類用來(lái)完成數(shù)據(jù)庫(kù)操作

 * */

public class PageInformation {

             private int pageSize;//每一頁(yè)包含的條目個(gè)數(shù)

             private int totalRows;//一共有多少行

             private int totalPages;//一共有多少頁(yè)

            

             private int currentPage=1;//初始化當(dāng)前頁(yè)為第一頁(yè)

             private boolean hasPrevious = false;//是否有前頁(yè),尚未使用,有待完善

             private boolean hasNext = false;//是否有后頁(yè),尚未使用,有待完善

            

             private ArrayList<User> list = new ArrayList<User>();//存放結(jié)果的列表

             private DatabaseGeneralServices dgs;//自己寫(xiě)的獲得connection的類,可以自己實(shí)現(xiàn)

             private Connection conn;          

             private PreparedStatement ps;

             private ResultSet rs;

            

             public PageInformation(){

              dgs = DatabaseGeneralServices.getInstance();

             pageSize = 20;//每頁(yè)設(shè)置為20

             totalRows = initRowCount();//得到總行數(shù),比較粗獷,不知有什么靈巧的方法,比如select count(*)...

             totalPages =((totalRows+pageSize)-1)/pageSize;//獲得總頁(yè)數(shù)

             initList(1);

                                           }

             /*

              * 每次查詢只取20條,封裝為一個(gè)列表,返回。參數(shù)是當(dāng)前頁(yè)號(hào),在構(gòu)造函數(shù)中默認(rèn)為第一頁(yè)

      * */

             private void initList(int currentPage){

                 list.removeAll(list);

             conn = dgs.getConnection();

             try{

                        ps = conn.prepareStatement("SELECT * FROM jsptest LIMIT ?,20");

                        int temp = (currentPage-1)*20;

                        ps.setInt(1, temp);

                        rs = ps.executeQuery();

                        while (rs.next()){

                                 User user = new User();

                                 user.setId(rs.getString(1));

                                 user.setName(rs.getString(2));

                                 list.add(user);

                        }

             }catch(SQLException e){

                        e.printStackTrace();

             }finally{

                        dgs.closeConnection(rs, ps, conn);

             }

             }

             //粗獷的得到行數(shù)

             private int initRowCount(){

             conn = dgs.getConnection();

             try{

             ps = conn.prepareStatement("SELECT * FROM jsptest");

             rs = ps.executeQuery();

             rs.last();

             int result = rs.getRow();

             rs.first();

             return result;

             }catch(SQLException e){

                        e.printStackTrace();

             }finally{

                        dgs.closeConnection(rs, ps, conn);

             }

             return 0;

             }

            

             //頁(yè)面調(diào)數(shù)據(jù)的時(shí)候,重新發(fā)出查詢,初始化結(jié)果列表

                   public ArrayList<User> getList(int cp) {

                            initList(cp);

                            ArrayList<User> temp = new ArrayList<User>();

                            for(int i =0;i<20;i++){

                                     temp.add(list.get(i));

                            }

                            return temp;

                   }

        //other setters and getters

                   ……

} 

緊接著是Action代碼

package com.ClockWise.ray.jsp;

import java.util.ArrayList;

import com.opensymphony.xwork2.ActionSupport;

public class ShowAction extends ActionSupport {

         private int totalPages;

         private boolean hasPre;

         private boolean hasNext;

         private int currentPage=1;

         private ArrayList<User> list;

         //以上幾個(gè)和PageInformation里面的一一對(duì)應(yīng)

         private String username ="ray";

         private ArrayList list2= new ArrayList();//這個(gè)list用來(lái)存放下面的頁(yè)碼起始位置

         private PageInformation pi;

        

         public String execute(){

                   init(currentPage);

                   return SUCCESS;

         }

        

         private void init(int currentPage){

                   pi = new PageInformation();

                   this.setTotalPages(pi.getTotalPages());

                   this.setHasPre(pi.isHasPrevious());

                   this.setHasNext(pi.isHasNext());

                   this.setList(pi.getList(currentPage));

                  

                   for(int i =currentPage;i<=currentPage+20;i++){

                            Current c = new Current();

                            c.setCurrentPage(i);

                            list2.add(c);

                   }

         }

        

         public ArrayList<User> getList() {

                   this.setList(pi.getList(currentPage));

                   return list;

         }

//other setters and getters

…….

} 

最后是JSP代碼

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

    pageEncoding="ISO-8859-1"%>

<%@ taglib prefix="s" uri="/struts-tags" %>

 

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<title>Insert title here</title>

</head>

<body>

<s:form action="ShowAction" method="GET">

   <h1>Welcome <s:property value="username"/></h1><BR>

   <h1>CurrentPage <s:property value="currentPage"/></h1>

  

   <!--show items of this page-->

   <s:iterator value="list" status="status">

        <s:property value="id"/>

        <s:property value="name"/>

        <BR>

   </s:iterator>

            

   <!--define the url of the previous page and next page-->

     <s:url id="url_pre" value="ShowAction.action">

         <s:param name="currentPage" value="currentPage-1"></s:param>

     </s:url>

     <s:url id="url_next" value="ShowAction.action">

         <s:param name="currentPage" value="currentPage+1"></s:param>

     </s:url> 

    

   <!-- use url defined above -->    

   <s:a href="%{url_pre}">Pre</s:a>

  

   <s:iterator value="list2" status="status">

      <s:url id="url" value="ShowAction.action">

      <!-- pass the currentPage parameter -->

         <s:param name="currentPage" value="currentPage"></s:param>

      </s:url>

      <s:a href="%{url}"><s:property value="currentPage"/>&nbsp</s:a>

   </s:iterator>

   <s:a href="%{url_next}">Next</s:a>

</s:form>  

</body>

</html>

 

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Struts 2 JSP分頁(yè),重構(gòu)版
JR - 精品文章 - 在struts中分頁(yè)的一種實(shí)現(xiàn)
[原創(chuàng)]分頁(yè)技巧(基于自定義標(biāo)簽 JSTL Struts) - Woden的專欄 - CS...
WEB開(kāi)發(fā)中Struts分頁(yè)算法
JAVA分頁(yè)實(shí)現(xiàn)(代碼)
Jsp中分頁(yè)功能的實(shí)現(xiàn)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服