Struts简单分页显示

Struts分页

*********************************************************************************************************************

执行过程在这里说明一下

首先  http://localhost:xxxx/xxx/index.jsp

然后单击   显示列表

会执行struts-config.xml  对应的  ation-mappings中action中的 path="/query"  type="xxx.QueryAction"

转到对应的QueryAction.java

如果执行成功 QueryAction就会返回  mapping.findWorad("succecc");

再到struts-config.xml   ation-mappings中action中的 path="/query"  type="xxx.QueryAction"中的

<forward name="success" path="/query.jsp" />

跳转到query.jsp

query.jsp  遍历会话的集合来显示数据  单击链接就会显示不同的内容

由于个人能力有限,如有不足,敬请谅解!!!!

*********************************************************************************************************************

重要用到ArrayList

Struts标签库logic /bean/html

模型PageBean

public   class  PageBean {

               
private  int curPage;//当前是第几页
               private  int rowCount=5;//每页显示多少行
               private  int maxPage;   //共有多少页
               private  int rowNum;//共有多少行信息
               private ArrayList data;// 所有信息
               private ArrayList  pageData;当前页面显示信息
                
                
public int getCurPage(){
                          
return curPage;
                 }

                
public void setCurPage(int curPage){
                     
this.curPage=curPage;
                }

                
public  int  getMaxPage(){
                           
return maxPage;
                }

                
public int getRowNum(){
                          
return rowNum;
                 }

                 
public void setRowNum(){
                     rowNum
=data.size();
                  }

                
public void setMaxPage(){
                            
if(data.size()%rowCount==0){
                                  maxPage
=data.size/rowCount;//如果没有余数,说明所有信息能在当前显示完
                             }
else{
                                  maxPage
=data.size/rowCount+1;//如果有余数,说明所有信息不能在当前显示完,所以应加上一页才可以显示所有信息

                            }

                }

                
public ArrayList  getData(){
                         
return   data;
                 }

                 
public void setData(ArrayList arrayList){
                          data
=arrayList;
                 }

                
public ArrayList getPageData(){
                     
return   pageData;
                }

                
public  void setPageData(){
                         pageData
=new ArrayList();
                        
for(int i=rowCount*(curPage-1);i<rowCount*curPage;i++){
                                
if(i==data.size()){
                                       
break;
                                 }

                                pageData.add(data.get(i));
                        }

              }


}
 

 

DataBaseOperator 执行数据库操作返回ArrayList集合为PageBean所用



import  java.sql. * ;
import  java.util. * ;

public   class  DataBaseOperator {
    
    Connection conn 
= null;
    Statement stmt 
= null;
    PreparedStatement pstmt 
= null;
        ResultSet rs 
= null;

 public UserBean(DataSource ds){
     try{
      conn = ds.getConnection();
     }catch(SQLException e){
      e.printStackTrace();
     }
    }

 public ArrayList selectemd(){
        String strsql 
= " xxxxxx";
        ArrayList ay 
= new ArrayList();
        
int i = 0;
        
try{
            xxx
= conn.xxxxx();
            rs 
= xxx.executeQuery();          //返回记录集
            
            
while(rs.next()){
               xxxx qf 
= new xxx();               //定义一个bean和数据库对应字段
               qf.setId(rs.getInt(1));
                                  qf.setName(rs.getString(
2));
                                 qf.setAge(rs.getInt(
3));
                                 qf.setSex(rs.getString(
4));
                                 qf.setDep(rs.getString(
5));
                                 qf.setDepname(rs.getString(
6));
                                 qf.setWork(rs.getString(
7));
                                 qf.setWorkname(rs.getString(
8));
               ay.add(i,qf);                     
//将bean绑定到集合中
               i++;
           }

        }
catch(SQLException e){
            e.printStackTrace();
        }

        
return ay;                 //返回集合
    }

    .......
}


    ...............................
        

 

QueryAction.java   

 

import  java.util.ArrayList;
import  java.util.HashMap;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  javax.sql.DataSource;

import  org.apache.struts.action.Action;

public   class  QueryAction  extends  Action  {

    
// --------------------------------------------------------- Instance Variables

    
// --------------------------------------------------------- Methods

    
/** 
     * Method execute
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@return ActionForward
     
*/

    
public ActionForward execute(
        ActionMapping mapping,
        ActionForm form,
        HttpServletRequest request,
        HttpServletResponse response) 
{
        DataSource ds 
= getDataSource(request,"sql");
        DataBaseOperator ob
= new DataBaseOperator (ds);
        ArrayList ay 
= ob.selectemd();
        ob.close();
         
int curPage=1;
        
if(ay.size() > 0){
            PageBean pb 
= new PageBean();
                      
if(request.getParamter("curPage")!=null){
                          curPage
=Integer.parserInt(request.getParamter("curPage"));
                          
if(curPage==0){
                              curPage
=1;
                           }

                         
                      }
               pb.setCurPage(curPage);
            pb.setData(ay);
               pb.setMaxPage();
            pb.setMaxPage();
            pb.setRowNum();
            request.getSession().setAttribute(
"pb",pb);
            
return mapping.findForward("success");
        }

        
// TODO Auto-generated method stub
        return null;
    }


}

struts-config.xml

 

<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >

< struts-config >
  
< data-sources  >
    
< data-source  key ="sql"  type ="org.apache.commons.dbcp.BasicDataSource" >
      
< set-property  property ="password"  value ="1973"   />
      
< set-property  property ="minCount"  value =""   />
      
< set-property  property ="maxCount"  value =""   />
      
< set-property  property ="username"  value ="sa"   />
      
< set-property  property ="driverClassName"  value ="com.microsoft.jdbc.sqlserver.SQLServerDriver"   />
      
< set-property  property ="description"  value ="sqlserver"   />
      
< set-property  property ="url"  value ="jdbc:microsoft:sqlserver://localhost:1433;databasename=xxx"   />
      
< set-property  property ="readOnly"  value ="false"   />
      
< set-property  property ="autoCommit"  value ="false"   />
    
</ data-source >

  
</ data-sources >

  ......
  
< action-mappings  >
    
    
< action  path ="/query"  
      type
="com.xxx.QueryAction" >
      
< forward  name ="success"  path ="/query.jsp"   />
    
</ action >
   
  
 
</ action-mappings >

 
  ............
</ struts-config >

 index.jsp

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html>
  <head>
    
    
    <title>My JSP 'index.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
   
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
  </head>
 
  <body>

    <html:link page="/query.do">
       显示列表
    </html:link>
  </body>
</html:html>

query.jsp

 
<%@ page language="java" pageEncoding="gb2312"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
 
<html>
 <head>
  <title>JSP for queryForm form</title>
 </head>
 <body>
  <h1>列表
    </h1>
    <html:errors />

    <table width="700"
      border="1" style="background:lavender;border-color:blue" cellpadding="5">
      <tr >
        <th>xxx</th>
        <th>xxx</th>
        <th>xxx</th>
              </tr>
      <!-- iterate over the results of the query -->
      <bean:define id="ay" name="pb" property="pageData"></bean:define>
      <logic:iterate id="element" name="ay">
        <tr bgColor="#eef4f9" align="center">
     <td>
           <bean:write name="element" property="id" />
     </td>
     <td>
           <bean:write name="element" property="name" />
     </td>
     <td>
           <bean:write name="element" property="age" />
     </td>
     <td>
               </td>
     <td> 
           <bean:write name="element" property="depname" />
     </td>
     <td>
           <bean:write name="element" property="workname" />
     </td>
        </tr>
      </logic:iterate>
    <tr>
         共<bean:write name="pb" property="maxPage"/>页 第<bean:write name="pb" property="curPage"/>页 共<bean:write name="pb" property="rowNum"/>条信息
        <logic:notEqual name="pb" property="curPage" value="1">
            <html:link page="/page.do?curPage=${pb.curPage - 1}">上一页</html:link>
        </logic:notEqual>
        <logic:notEqual name="pb" property="curPage" value="${pb.maxPage}">
            <html:link page="/page.do?curPage=${pb.curPage + 1}">下一页</html:link>
        </logic:notEqual>
    </tr>
    </table>
 </body>
</html>

*************************************************************************************************************

 执行过程在这里说明一下

首先  http://localhost:xxxx/xxx/index.jsp

然后单击   显示列表

会执行struts-config.xml  对应的  ation-mappings中action中的 path="/query"  type="xxx.QueryAction"

转到对应的QueryAction.java

如果执行成功 QueryAction就会返回  mapping.findWorad("succecc");

再到struts-config.xml   ation-mappings中action中的 path="/query"  type="xxx.QueryAction"中的

<forward name="success" path="/query.jsp" />

跳转到query.jsp

query.jsp  遍历会话的集合来显示数据  单击链接就会显示不同的内容

由于个人能力有限,如有不足,敬请谅解!!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值