昨天在找电影的时候偶然发现一年多前写的一个分页程序。拿出来分享下。代码比较简陋。也比较简单,对于初学者来说是个不错的分页例子。呵呵。好像还有一个BUG。
public class Student {
private String name;
private int age;
private int score;
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public Student(String name,int age, int score) {
super();
this.age = age;
this.name = name;
this.score = score;
}
public Student[] getStu(){
Student [] stu =new Student[]{new Student("张三 ",18,80),
new Student("李四 ",18,80),
new Student("王五",18,80),
new Student("赵六",18,80),
new Student("孙琦 ",18,80),
new Student("刘八",18,80),
new Student("武松",18,80),
new Student("项羽 ",18,80)};
return stu;
}
}
===================jsp部分=====================================
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="com.page.Student"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<body>
<table border=1 width="300px">
<%
response.setCharacterEncoding("GBK");
Student results = new Student();
Student [] stu = results.getStu();
int p;//指定当前页
int size = results.getStu().length;
int pagecount = size/3+1;
String count=request.getParameter("page");
if(count==null)
{
p=1;
}
else
{
p=Integer.parseInt(count);
}
if(p==1)
{%>
<font size=2>第一页</font>
<font size=2>上一页</font>
<%}
else
{%>
<a href="index.jsp?page=1">第一页</a>
<a href="index.jsp?page=<%=p-1 %>">上一页</a>
<%}
if(p==pagecount)
{%>
<font size=2>下一页</font>
<font size=2>最后页</font>
<%}
else
{%>
<a href="index.jsp?page=<%=p+1 %>">下一页</a>
<a href="index.jsp?page=<%=pagecount %>">最后页</a>
<%}
for(int x=1;x<=pagecount;x++)//循环分页记录
{
if(p==x)
{ for(int i=(p-1)*3;i<=p*3;i++)//循环取出每页记录
{
if(i<stu.length){
%>
<tr><td><%=stu[i].getName()%></td><td><%=stu[i].getAge()%></td><td><%=stu[i].getScore()%></td></td></tr>
<%}}}}%>
</tr>
</table>
</body>
</html>