有热心网友回复:
str += " 转到<select name='page' onchange=\"window.location.href='" + filename + temp + "cur_page='+this.options[this.selectedindex].value\">";
已经试过了,没问题

1.以下是实现分页的类pageresultset

复制代码 代码如下:

package page.bean;
import java.util.*;
public class pageresultset {
/**
* 分页数据
*/
private collection data = null;
/**
* 当前页
*/
private int curpage;
/**
* 每页显示的记录数
*/
private int pagesize;
/**
* 记录行数
*/
private int rowscount;
/**
* 页数
*/
private int pagecount;
public pageresultset(collection data) {
this.data = data;
this.curpage = 1;
this.pagesize = 10;
this.rowscount = data.size();
this.pagecount = (int) math.ceil((double) rowscount / pagesize);
}
public pageresultset(collection data, int curpage) {
this.data = data;
this.curpage = curpage;
this.pagesize = 10;
this.rowscount = data.size();
this.pagecount = (int) math.ceil((double) rowscount / pagesize);
}
public pageresultset(collection data, int curpage, int pagesize) {
this.data = data;
this.curpage = curpage;
this.pagesize = pagesize;
this.rowscount = data.size();
this.pagecount = (int) math.ceil((double) rowscount / pagesize);
}
/**
* getcurpage:返回当前的页数
*
* @return int
*/
public int getcurpage() {
return curpage;
}
/**
* getpagesize:返回分页大小
*
* @return int
*/
public int getpagesize() {
return pagesize;
}
/**
* getrowscount:返回总记录行数
*
* @return int
*/
public int getrowscount() {
return rowscount;
}
/**
* getpagecount:返回总页数
*
* @return int
*/
public int getpagecount() {
return pagecount;
}
/**
* 第一页
*
* @return int
*/
public int first() {
return 1;
}
/**
* 最后一页
*
* @return int
*/
public int last() {
return pagecount;
}
/**
* 上一页
*
* @return int
*/
public int previous() {
return (curpage - 1 < 1) ? 1 : curpage - 1;
}
/**
* 下一页
*
* @return int
*/
public int next() {
return (curpage + 1 > pagecount) ? pagecount : curpage + 1;
}
/**
* 第一页
*
* @return boolean
*/
public boolean isfirst() {
return (curpage == 1) ? true : false;
}
/**
* 最后一页
*
* @return boolean
*/
public boolean islast() {
return (curpage == pagecount) ? true : false;
}
/** * 获取当前页数据
*
* @return collection
*/
public collection getdata() {
collection curdata = null;
if (data != null) {
int start = (curpage - 1) * pagesize;
int end = 0;
if (start + pagesize > rowscount)
end = rowscount;
else
end = start + pagesize;
arraylist arraycurdata = new arraylist();
arraylist arraydata = null;
vector vectorcurdata = new vector();
vector vectordata = null;
boolean isarray = true;
if (data instanceof arraylist) {
arraydata = (arraylist) data;
isarray = true;
} else if (data instanceof vector) {
vectordata = (vector) data;
isarray = false;
}
for (int i = start; i < end; i++) {
if (isarray) {
arraycurdata.add(arraydata.get(i));
} else {
vectordata.add(vectordata.elementat(i));
}
}
if (isarray) {
curdata = (collection) arraycurdata;
} else {
curdata = (collection) vectorcurdata;
}
}
return curdata;
}
/**
* 获取工具条
*
* @return string
*/
public string gettoolbar(string filename) {
string temp = "";
if (filename.indexof("?") == -1) {
temp = "?";
} else {
temp = "&";
}
string str = "<form method='post' name='frmpage' action='" + filename + "'>";
str += "<p align='center'>";
if (isfirst())
str += "首页 上一页 ";
else {
str += "<a href='" + filename + temp + "cur_page=1'>首页</a> ";
str += "<a href='" + filename + temp + "cur_page=" + (curpage - 1) + "'>上一页</a> ";
}
if (islast())
str += "下一页 尾页 ";
else {
str += "<a href='" + filename + temp + "cur_page=" + (curpage + 1) + "'>下一页</a> ";
str += "<a href='" + filename + temp + "cur_page=" + pagecount + "'>尾页</a> ";
}
str += " 共<b>" + rowscount + "</b>条记录 ";
str += " 转到<select name='page' onchange="location='" + filename
+ temp + "cur_page='+this.options[this.selectedindex].value">";
for (int i = 1; i <= pagecount; i++) {
if (i == curpage)
str += "<option value='" + i + "' selected>第" + i + "页</option>";
else
str += "<option value='" + i + "'>第" + i + "页</option>";
}
str += "</select></p></form>";
return str;
}
}

2.一下是action
复制代码 代码如下:

/*
* generated by myeclipse struts
* template path: templates/java/javaclass.vtl
*/
package struts.action;
import java.util.collection;
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 page.bean.databaseconn;
import page.bean.pageresultset;
import page.bean.contactbo;
import struts.form.loginform;
/**
* myeclipse struts
* creation date: 04-02-2008
*
* xdoclet definition:
* @struts.action path="/login" name="loginform" input="/login.jsp" scope="request" validate="true"
* @struts.action-forward name="sss" path="/index.jsp"
*/
public class loginaction extends action {
/*
* generated methods
*/
/**
* method execute
* @param mapping
* @param form
* @param request
* @param response
* @return actionforward
*/
public actionforward execute(actionmapping mapping, actionform form,
httpservletrequest request, httpservletresponse response) {
loginform loginform = (loginform) form;
contactbo userbo=new contactbo();
//先从业务处理逻辑类中取出数据(arraylist或vector格式)
collection data;
try {
data = userbo.findcontact(databaseconn.getconnection());
//再得到当前页curpage和每页记录数pagesize
//int curpage = integer.parseint((string)request.getparameter("cur_page"));
int curpage = 1;
string cur = request.getparameter("cur_page");
system.out.println("--------------: "+cur);
if(cur!=null && cur !=""){
curpage = new integer(cur).intvalue();
}
int pagesize=10;
//然后生成pageresultset对象
pageresultset datalist = new pageresultset(data, curpage, pagesize);
request.setattribute("userslist", datalist);
return mapping.findforward("sss");
} catch (exception e) {
e.printstacktrace();
return mapping.getinputforward();
}
}
}

3.以下是显示分页的页面
复制代码 代码如下:

<%@ page language="java" import="java.util.*,page.bean.contact,page.bean.pageresultset"
pageencoding="gb2312"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme() + "://"
+ request.getservername() + ":" + request.getserverport()
+ path + "/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>">
<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>
<table border="1">
<%
pageresultset pageresultset = (pageresultset) request.getattribute("userslist");
arraylist userslist = (arraylist) pageresultset.getdata();
for (int i = 0; i < userslist.size(); i++) {
contact co = (contact) userslist.get(i);
%>
<tr>
<td>
<%=co.getid() %>
</td>
<td>
<a href="login.do?id=<%=co.getid()%>"><%=co.getusername()%></a>
</td>
<td>
<%=co.getmobile() %>
</td>
<td>
<%=co.getmail() %>
</td>
<td>
<%=co.getphone() %>
</td>
<td>
<%=co.getmem() %>
</td>
<td>
<%=co.getlastcontact() %>
</td>
</tr>
<%
}
%>
</table>
<!-- 显示分页工具栏 -->
<%=pageresultset.gettoolbar("login.do")%>
</body>
</html>

上一篇: 惠普商用笔记本预装WIN10系统如何分区

下一篇: mybatis单笔批量保存实体数据的方法

推荐阅读