struts分页实例代码
来源:互联网 发布:潮牌顶级复刻的淘宝店 编辑:程序博客网 时间:2024/05/22 06:36
//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl
package com.yourcompany.struts.action;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.bean.Book;
import com.yourcompany.struts.bean.PageBean;
import com.yourcompany.struts.form.PageListForm;
/**
* MyEclipse Struts
* Creation date: 07-28-2006
*
* XDoclet definition:
* @struts.action path="/pageList" name="pageListForm" input="/pageList.jsp" scope="request"
*/
public class PageListAction extends Action {
// --------------------------------------------------------- Instance Variables
// --------------------------------------------------------- Methods
ArrayList arrayList=new ArrayList();
PageBean pd;
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
//PageListForm pageListForm = (PageListForm) form;
String action;
action=request.getParameter("action");
if(action==null||action.equals("null")) {
try {
arrayList=Book.getAllBook();
}
catch(Exception e) {
e.printStackTrace();
}
pd=new PageBean(arrayList);
Book[] books=pd.getBooks();
request.setAttribute("result",books);
request.setAttribute("page",pd);
}
else {
if(action=="nextPage"||action.equals("nextPage")) {
Book[] books=pd.getNextPage();
request.setAttribute("result",books);
request.setAttribute("page",pd);
}
if(action=="previousPage"||action.equals("previousPage")) {
Book[] books=pd.getPreviousPage();
request.setAttribute("resule",books);
request.setAttribute("page",pd);
}
}
return mapping.findForward("success");
}
}
package com.yourcompany.struts.bean;
import java.sql.*;
import java.util.ArrayList;
public class Book {
private String bookname;
private String author;
private String price;
public Book(String name,String author,String price) {
this.bookname=name;
this.author=author;
this.price=price;
}
public void setBookname(String bookname) {
this.bookname=bookname;
}
public void setAuthor(String Author) {
this.author=author;
}
public void setPrice(String price) {
this.price=price;
}
public String getBookname() {
return bookname;
}
public String getAuthor() {
return author;
}
public String getPrice() {
return price;
}
public static ArrayList getAllBook() throws Exception {
String sql="select * from book";
SqlBean sq=new SqlBean();
ArrayList arrayList=new ArrayList();
try
{
ResultSet resultSet=sq.select(sql);
while(resultSet.next()) {
String name=resultSet.getString("bookname");
String author=resultSet.getString("author");
String price=resultSet.getString("price");
Book book=new Book(name,author,price);
arrayList.add(book);
}
resultSet.close();
}
catch(SQLException e)
{
System.out.println("数据库错误"+e.toString());
}
return arrayList;
}
}
package com.yourcompany.struts.bean;
import com.yourcompany.struts.bean.Book;
import java.util.*;
public class PageBean {
int currentPage=1;//当前页数
public int totalPages=0;//总页数
int pageRecorders=2;//每页显示数
int totalRows=0;//总数据数
int pageStartRow=0;//每页的起始数
int pageEndRow;//每页的终止数
boolean hasNextPage=false;//是否有下一页
boolean hasPreviousPage=false;//是否有前一页
ArrayList arrayList;
Iterator it;
public PageBean(ArrayList arrayList) {
this.arrayList=arrayList;
totalRows=arrayList.size();
it=arrayList.iterator();
hasPreviousPage=false;
currentPage=1;
if((totalRows%pageRecorders)==0) {
totalPages=totalRows/pageRecorders;
}
else {
totalPages=totalRows/pageRecorders+1;
}
if(currentPage>=totalPages) {//当前页数是否大于总页数
hasNextPage=false;//如果是,没下一页
}
else {
hasNextPage=true;//有下一页
}
if(totalRows<pageRecorders) {//总数据数小于每页显示数
this.pageStartRow=0;//每页起始数0
this.pageEndRow=totalRows;//每页终止数为总数据数
}
else {
this.pageStartRow=0;
this.pageEndRow=pageRecorders;//每页终止数为每页显示数
}
}
public void setCurrentPage(int currentPage) {
this.currentPage=currentPage;
}
public void setPageRecorders(int pageRecorders) {
this.pageRecorders=pageRecorders;
}
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage=hasNextPage;
}
public void setHasPreviosPage(boolean hasPreviosPage) {
this.hasPreviousPage=hasPreviousPage;
}
public String getCurrentPage() {
return this.toString(currentPage);
}
public String getTotalPages() {
return this.toString(totalPages);
}
public String getTotalRow() {
return this.toString(totalRows);
}
public int getPageRecorders() {
return pageRecorders;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageStartRow() {
return pageStartRow;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public Book[] getNextPage() {
currentPage=currentPage+1;
if((currentPage-1)>0) {//是否为第一页
hasPreviousPage=true;
}
else {
hasPreviousPage=false;//是
}
if(currentPage>=totalPages) {//是否为最后一页
hasNextPage=false;
}
else {
hasNextPage=true;
}
Book[] books=getBooks();
return books;
}
public Book[] getPreviousPage() {
currentPage=currentPage-1;
if(currentPage==0) {
currentPage=1;
}
if(currentPage>=totalPages) {
hasNextPage=false;
}
else {
hasNextPage=true;
}
if((currentPage-1)>0) {
hasPreviousPage=true;
}
else {
hasPreviousPage=false;
}
Book[] books=getBooks();
return books;
}
public Book[] getBooks() {
if(currentPage*pageRecorders<totalRows) {
pageEndRow=currentPage*pageRecorders;
pageStartRow=pageEndRow-pageRecorders;
}
else {
pageEndRow=totalRows;
pageStartRow=pageRecorders*(totalPages-1);
}
Book[] books=new Book[pageEndRow-pageStartRow+1];
int j=0;
for(int i=pageStartRow;i<pageEndRow;i++) {
Book book=(Book)arrayList.get(i);
books[j++]=book;
}
return books;
}
public String toString(int temp) {
String str=Integer.toString(temp);
return str;
}
}
package com.yourcompany.struts.bean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SqlBean {
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=login";
Connection con=null;
Statement sta=null;
public SqlBean() {
try
{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con=DriverManager.getConnection(url,"sa","");
sta=con.createStatement();
}
catch(Exception e)
{
System.out.println("连接错误"+e.toString());
}
}
public ResultSet select(String selects) throws Exception
{
return sta.executeQuery(selects);
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import="com.yourcompany.struts.bean.*;"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html locale="true">
<head>
<html:base />
<title>pagetest.jsp</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">
</head>
<body>
<table border="1">
<tr><th>书名</th><th>作者</th><th>价格</th></tr>
<logic:present name="result">
<logic:iterate id="book" name="result" type="Book">
<logic:present name="book">
<tr>
<td><bean:write name="book" property="bookname"/></td>
<td><bean:write name="book" property="author"/></td>
<td><bean:write name="book" property="price"/></td>
</tr>
</logic:present>
</logic:iterate>
</logic:present>
</table>
<logic:present name="page">
<logic:equal name="page" property="hasNextPage" value="true">
<html:link page="/pageList.do?action=nextPage">nextPage</html:link>
</logic:equal>
<logic:equal name="page" property="hasPreviousPage" value="true">
<html:link page="/pageList.do?action=previousPage">previousPage</html:link>
</logic:equal>
共分<bean:write name="page" property="totalPages"/>页显示,当前是
<bean:write name="page" property="currentPage"/>页.
</logic:present>
</body>
</html:html>
- struts分页实例代码
- Struts 数据分页讲解与代码实例
- struts中的分页实例
- Hibernate+Struts分页代码
- Struts分页代码
- Hibernate+Struts分页代码
- Hibernate+Struts分页代码
- struts 标签 分页代码
- Struts + Hibernate 代码实例
- struts下的分页代码
- Struts 2+Hibernate分页代码
- Struts 2+Hibernate分页代码
- JSP分页代码实例
- JAVA分页代码实例
- JSP分页代码实例
- Jsp分页实例代码
- JAVA分页代码实例
- PHP分页实例代码
- [Tip] Log4j的配置
- DateAdd 函数
- 详细解说STL string
- c 语言的 static
- 关于xajax的中文编码问题
- struts分页实例代码
- WAP开发常见问题汇总篇
- Hibernate笔记
- 年月日三联动下拉框
- 检测手机对MMAPI支持的情况
- 嵌入式处理器分类与现状
- JavaScript
- 评论:互联网络业的十大发展趋势(下)
- .NET程序中常用的代码(转自Shadow的专栏)