龙行天下

  政 博
随笔 - 23, 文章 - 0, 评论 - 12, 引用 - 0
数据加载中……

struts中的分页实例

struts中的分页实例 
  
    这仅是一个入门实例,简洁实用,从网上整理而来,供参考。这里仅列出部分文件,其它文件、数据库及目录结构请从本站下载。
一、struts-config.xml配置,主要是配数据库,这里用access。
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_2.dtd">


<struts-config>
<data-sources>
   <data-source key="dataSource" type="org.apache.commons.dbcp.BasicDataSource">
            <set-property property="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver" />
            <set-property property="url" value="jdbc:odbc:page" />
            <set-property property="username" value="admin" />
            <set-property property="password" value="" />
            <set-property property="maxActive" value="20" />
            <set-property property="maxWait" value="5000" />
            <set-property property="defaultAutoCommit" value="true" />
            <set-property property="defaultReadOnly" value="false" />
            <set-property property="validationQuery" value="SELECT 1" />
            <set-property property="removeAbandoned" value="true" />
            <set-property property="removeAbandonedTimeout" value="120" />            
            <set-property property="encoding" value="false" />            
        </data-source>

 </data-sources>
  <form-beans>
  </form-beans>
  <global-forwards>
  </global-forwards>

  <action-mappings>
  <action path="/page" type="page.DataSourceAction" scope="request">
  <forward name="success" path="/pagetest.jsp"/>
  </action>
  </action-mappings>

  <controller>
  </controller>
</struts-config>

二、Action类
用 page.do?start=1 来显示第一个页面 
参数说明: 
  list:信息列表 
  start:开始位置 
  page:每页显示的信息数目 
  pages: 总页数
  previous:上页开始位置 
  next:下页开始位置 

package page;
import org.apache.struts.action.*;
import javax.servlet.http.*;

import java.util.*;
import javax.sql.*;
import java.sql.*;
import bean.*;

public class DataSourceAction extends Action {

 public DataSourceAction(){}
 
 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
 HttpServletResponse response) throws Exception {   
  try{
      DataSource ds=this.getDataSource(request,"dataSource");   
      Connection con = ds.getConnection();
      Statement stmt = con.createStatement();
      ResultSet resultSet = stmt.executeQuery("select count(*) from book" ); 
      resultSet.next(); 
      int data_num=resultSet.getInt(1); 
      int start=1;
      int page = 4; //每页的记录数。
      int pages=data_num/page;
      if(data_num%page!=0)
         pages++;
      if(request.getParameter("start")!=null)
          start = Integer.parseInt(request.getParameter("start")); 
      if(request.getParameter("go")!=null){
                int go = Integer.parseInt(request.getParameter("go")); 
                if(go<=1)
                  start=1;
                else if(go>pages)
                  start=(pages-1)*page+1;
                else 
                   start=(go-1)*page+1;
       } 
     
      String sql = "SELECT * FROM book where id>="+start+" and id<"+(start+page);
      resultSet = stmt.executeQuery(sql);  
      ArrayList list = new java.util.ArrayList(); 
      while(resultSet.next())
      {  
        int id=resultSet.getInt("id");
        String name = resultSet.getString("name");
        String author = resultSet.getString("author");
        String price = resultSet.getString("price");
        System.out.println("开始数据封装:name="+name+"author="+author+"price="+price);
        Book book= new Book(id,name,author,price);       
        list.add(book);
      }
    con.close();
    request.setAttribute("pages",new Integer(pages));
    request.setAttribute("list",list);
    //request.setAttribute("start", new Integer(start)); 
    request.setAttribute("page", new Integer(page)); 

        // if there is a previous page, set the previous variable 
        int previous = start-page; 
        if ( previous>=0 ){ 
            request.setAttribute("previous", new Integer(previous)); 
            System.out.println ("previous:" + previous); 
        } 

        // if there is a next page, set the next variable 
        int next = start+page; 
        if ( next<=data_num ){ 
            request.setAttribute("next", new Integer(next)); 
            System.out.println ("next:" + next); 
        } 

   }catch(SQLException e){
          e.printStackTrace();
          System.out.println("数据库连接出现异常");
      } 
   
             return (mapping.findForward("success"));
  } 
}


三、bean类Book.java
package bean;
import java.sql.*;
import java.util.ArrayList;
public class Book {
 int id;
 private String bookname; //书名
 private String author;   //作者
 private String price;    //价格
 
public Book(int id,String name,String author,String price){
 this.id=id;
 this.bookname=name;
 this.author=author;
 this.price=price;
}

public int getId(){ 
        return id; 
    } 

 public String getAuthor() {
  return author;
 }

 public void setAuthor(String author) {
  this.author = author;
 }

 public String getBookname() {
  return bookname;
 }

 public void setBookname(String bookname) {
  this.bookname = bookname;
 }
 
 public String getPrice(){
     return this.price; 
 }
 
 public void setPrice(String price){
     this.price=price; 
 }
 
}
四、分页的jsp页面pagetest.jsp,用了jstl中的c标记。
<%@ page contentType="text/html; charset=gb2312" language="java"%>
<%@ page import="java.util.*" %>
<%@ page import="bean.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>

<logic:iterate id="book" name="list" type="bean.Book"> 
            <br><html:link  
                    paramId="id" paramName="book" paramProperty="id" 
                    page="/messagedetail.do"> 
                    <bean:write name="book" property="bookname" /> 
                </html:link> 
</logic:iterate> 

<form action="/TestPage/page.do">
   <logic:present name="previous"> 
            <html:link  
                    paramId="start" paramName="previous" 
                    page="/page.do"> 
                    上一页 
                </html:link> 
   </logic:present> 

   <logic:present name="next"> 
              <html:link  
                    paramId="start" paramName="next" 
                    page="/page.do"> 
                    下一页 
                </html:link> 
   </logic:present> 

每页<c:out value="${page}"/>条记录,共<c:out value="${pages}"/>页  跳到<input type="text"  name="go" size="3" maxlength="30" >
<input type="submit" value="go" >
</form>
         

 

OK!!!!
五、测试,请下载本实例的目录结构TestPage,放入tomcat的webapps下,在浏览器中输入:
    http://127.0.0.1:8080/TestPage/page.do 
来源: java学习室
 

posted on 2006-05-03 20:04 feingto 阅读(619) 评论(0)  编辑  收藏 所属分类: Java Web


只有注册用户登录后才能发表评论。


网站导航: