Some enthusiastic netizens replied:
Str + = "go to <select name = 'page' onChange = \" window. location. href = '"+ fileName + temp +" cur_page =' + this. options [this. selectedIndex]. value \ "> ";
I already tried it. No problem
1. The following is a pagination class PageResultSet.
Copy codeThe Code is as follows:
Package page. bean;
Import java. util .*;
Public class PageResultSet {
/**
* Paging data
*/
Private Collection data = null;
/**
* Current page
*/
Private int curPage;
/**
* Number of records displayed on each page
*/
Private int pageSize;
/**
* Number of records
*/
Private int rowsCount;
/**
* Page number
*/
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: returns the current page number.
*
* @ Return int
*/
Public int getCurPage (){
Return curPage;
}
/**
* GetPageSize: returns the page size.
*
* @ Return int
*/
Public int getPageSize (){
Return pageSize;
}
/**
* GetRowsCount: Total number of returned records
*
* @ Return int
*/
Public int getRowsCount (){
Return rowsCount;
}
/**
* GetPageCount: Total number of returned pages
*
* @ Return int
*/
Public int getPageCount (){
Return pageCount;
}
/**
* Page 1
*
* @ Return int
*/
Public int first (){
Return 1;
}
/**
* Last Page
*
* @ Return int
*/
Public int last (){
Return pageCount;
}
/**
* Previous Page
*
* @ Return int
*/
Public int previous (){
Return (curPage-1 <1 )? 1: curPage-1;
}
/**
* Next Page
*
* @ Return int
*/
Public int next (){
Return (curPage + 1> pageCount )? PageCount: curPage + 1;
}
/**
* Page 1
*
* @ Return boolean
*/
Public boolean isFirst (){
Return (curPage = 1 )? True: false;
}
/**
* Last Page
*
* @ Return boolean
*/
Public boolean isLast (){
Return (curPage = pageCount )? True: false;
}
/*** Get the current page data
*
* @ 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;
}
/**
* Get tool bar
*
* @ 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 + = "homepage previous ";
Else {
Str + = "<a href = '" + fileName + temp + "cur_page = 1'> homepage </a> ";
Str + = "<a href = '" + fileName + temp + "cur_page =" + (curPage-1) + "'> previous page </a> ";
}
If (isLast ())
Str + = "next last page ";
Else {
Str + = "<a href = '" + fileName + temp + "cur_page =" + (curPage + 1) + "'> next page </a> ";
Str + = "<a href = '" + fileName + temp + "cur_page =" + pageCount + "'> last page </a> ";
}
Str + = "Total <B>" + rowsCount + "</B> records ";
Str + = "go to <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> page" + I + "</option> ";
Else
Str + = "<option value = '" + I + "'> page" + I + "</option> ";
}
Str + = "</select> </p> </form> ";
Return str;
}
}
2. Here is Action.
Copy codeThe Code is as follows:
/*
* 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 ();
// Extract data from the business processing logic class first (ArrayList or Vector format)
Collection data;
Try {
Data = userBO. findContact (DatabaseConn. getConnection ());
// Obtain the current page curPage and the number of records per page 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;
// Then generate the PageResultSet object
PageResultSet dataList = new PageResultSet (data, curPage, pageSize );
Request. setAttribute ("usersList", dataList );
Return mapping. findForward ("sss ");
} Catch (Exception e ){
E. printStackTrace ();
Return mapping. getInputForward ();
}
}
}
3. The following page is displayed.
Copy codeThe Code is as follows:
<% @ 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>
<! -- Display pagination toolbar -->
<% = PageResultSet. getToolBar ("login. do") %>
</Body>
</Html>