共分为以下几部分,是我在struts项目中的案例——————
1、page 的javascript脚本
function mypage()
{
this.frm;
this.init = initPage;
this.nextPage = nextPage;
this.previousPage = previousPage;
this.firstPage = firstPage;
this.lastPage = lastPage;
this.goPage = goPage;
function initPage(frmname)
{
this.frm = document.forms[frmname];
}
function nextPage()
{
if(this.frm.elements["curentPage"].value==this.frm.elements["pageCount"].value);
else {
this.frm.elements["curentPage"].value=parseInt(this.frm.elements["curentPage"].value)+1;
this.frm.elements["action"].value="pagequery";
var frm = this.frm;
frm.submit();
}
}
function previousPage()
{
if(this.frm.elements["curentPage"].value==1||this.frm.elements["curentPage"].value==0);
else {
this.frm.elements["curentPage"].value=parseInt(this.frm.elements["curentPage"].value)-1;
this.frm.elements["action"].value="pagequery";
var frm = this.frm;
frm.submit();
}
}
function firstPage()
{
if(this.frm.elements["curentPage"].value==1||this.frm.elements["curentPage"].value==0);
else {
this.frm.elements["curentPage"].value=parseInt(1);
this.frm.elements["action"].value="pagequery";
var frm = this.frm;
frm.submit();
}
}
function lastPage()
{
if(this.frm.elements["curentPage"].value==this.frm.elements["pageCount"].value);
else {
this.frm.elements["curentPage"].value=parseInt(this.frm.elements["pageCount"].value);
this.frm.elements["action"].value="pagequery";
var frm = this.frm;
frm.submit();
}
}
function goPage()
{
if (!isNumber(this.frm.elements["pageNO"].value)||this.frm.elements["pageNO"].value==0){
alert("请正确输入跳转的页码!");
this.frm.elements["pageNO"].select();
return;
}
else {if(parseInt(this.frm.elements["pageNO"].value)>parseInt(this.frm.elements["pageCount"].value))
{
alert("输入跳转的页码大于总页数!");
this.frm.elements["pageNO"].select();
return;
}
else if(parseInt(this.frm.elements["pageNO"].value)==parseInt(this.frm.elements["curentPage"].value))
return;
}
this.frm.elements["curentPage"].value=parseInt(this.frm.elements["pageNO"].value);
this.frm.elements["action"].value="pagequery";
var frm = this.frm;
frm.submit();
}
}
function isNumber( s ){
var regu = "^[0-9]+$";
var re = new RegExp(regu);
if (s.search(re) != -1) {
return true;
} else {
return false;
}
}
2、页面(主要是红字部分):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<html:html>
<HEAD>
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<META http-equiv="Content-Type" content="text/html; charset=GB2312">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<link rel="stylesheet" href="/dcdy/common/main.css" type="text/css">
<link rel="stylesheet" href="/dcdy/common/business_table.css" type="text/css">
<TITLE></TITLE>
</HEAD>
<script language="JavaScript" src="/dcdy/pagination.js"></script>
<BODY bgcolor=#FFFFFF >
<html:form action="/rejectList.do" method="post">
<html:hidden property="action" value="query"/>
<html:hidden property="curentPage" />
<html:hidden property="pageCount" />
<html:hidden property="onePageNum" />
<html:hidden property="recordNum" />
<script>
var myPage = new mypage();
myPage.init("0");
</script>
<table width="618" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td width=6 background="/dcdy/images/r1_011.jpg" height="12"></td>
<td valign=top align=left background=/dcdy/images/r1_031.jpg height=12></td>
<td width=6 background="/dcdy/images/r1_041.jpg"></td>
</tr>
<tr>
<td width=6 background="/dcdy/images/r1_05.jpg" height="1"></td>
<td valign=top align=center><br>
<table cellspacing=1 cellpadding=0 width="578" border=0 align="center" bgcolor=#7C93ED>
<caption class="tableCell"><font size="3">列表</font> </caption>
<tr bgcolor="#F7F7F7">
<td><div align="center">号</div></td>
<td><div align="center">操作员</div></td>
<td><div align="center">人</div></td>
<td><div align="center">原因</div></td>
<td colspan="2"><div align="center">操作</div></td>
</tr>
<logic:iterate name="rs" id="register" scope="request" type="java.util.HashMap">
<tr onMouseOver="this.style.backgroundColor='#FFFFFF'" onMouseOut="this.style.backgroundColor='#E2EEF8'" bgcolor='#E2EEF8' align='left' )" style="cursor: hand">
<td><bean:write name="register" property="pledgereg_id"/></td>
<!--td><bean:write name="register" property="mortgatername"/></td-->
<td><bean:write name="register" property="user_id"/></td>
<td><bean:write name="register" property="checkuser_id"/></td>
<td><bean:write name="register" property="bak"/></td>
<td align="center">
<a href="rejectList.do?action=edit&&key=<bean:write name="register" property="pledgereg_id"/>">修改</a>
<a href="rejectList.do?action=del&&key=<bean:write name="register" property="pledgereg_id"/>">删除</a>
</td>
</tr>
</logic:iterate>
<tr class="content1" bgcolor="#F7F7F7">
<td height="20" colspan="17">
<div align="left">
<a href="javascript:myPage.firstPage()">第一页</a>
<a href="javascript:myPage.nextPage()">下一页</a>
<a href="javascript:myPage.previousPage()">上一页</a>
<a href="javascript:myPage.lastPage()">最后一页</a>
第<script>document.write(document.forms[0].elements["curentPage"].value)</script>页 /
共<script>document.write(document.forms[0].elements["pageCount"].value)</script>页
跳转至
</font><INPUT type="text" name="pageNO" width="1" size="10">页<INPUT
name="Submit" type="button" id="Submit" value="跳转"
onclick="javascript:myPage.goPage();" style="cursor:hand;"></div>
</td>
</tr>
</table>
<p>
</td>
<td width=6 background="/dcdy/images/r1_07.jpg" height="1"></td>
</tr>
<tr>
<td width=6 background="/dcdy/images/r1_08.jpg" height="6"></td>
<td valign=top align=left background=/dcdy/images/r1_09.jpg height="6"></td>
<td width=6 background="/dcdy/images/r1_10.jpg" height="6"></td>
</tr>
</table>
</html:form>
</BODY>
</html:html>
<script>
function test(msg){
return confirm(msg);
}
</script>
3、后台java程序
接受类Action
public class CheckFinishList extends Action
{
public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm,
HttpServletRequest httpServletRequest, HttpServletResponse response)
throws Exception {
HttpServletRequest request = httpServletRequest;
Approvedao dao = new Approvedao();
String dept_id = request.getSession().getAttribute("dept_id").toString();
RePrintPageActionForm mqf = (RePrintPageActionForm) actionForm;
String action;
if(request.getParameter("action").equals("") || request.getParameter("action")==null)
{
action="query";
}
else
{
action=request.getParameter("action");
}
if(action.equals("query")||action.equals("fellback")||action.equals("del"))
{
List queryList=(List)dao.getAllregister(dept_id);
mqf.setRecordNum(queryList.size());
mqf.setOnePageNum(15);
if(mqf.getRecordNum()%mqf.getOnePageNum()==0)
mqf.setPageCount(mqf.getRecordNum() /mqf.getOnePageNum());
else mqf.setPageCount(mqf.getRecordNum()/mqf.getOnePageNum()+1);
if(mqf.getRecordNum()==0){
request.setAttribute("rs",new ArrayList());
mqf.setCurentPage(0);
return (actionMapping.findForward("view"));
}
if(Integer.toString(mqf.getCurentPage()).equals("0"))
mqf.setCurentPage(1);
List showList=dao.getShowList(queryList,mqf.getPageCount(),mqf.getCurentPage(),mqf.getOnePageNum(),mqf.getRecordNum());
request.setAttribute("rs",showList);
return (actionMapping.findForward("view"));
}
if(action.equals("pagequery"))
{
List queryList=(List)dao.getAllregister(dept_id);
mqf.setRecordNum(queryList.size());
mqf.setOnePageNum(15);
if(mqf.getRecordNum()%mqf.getOnePageNum()==0)
mqf.setPageCount(mqf.getRecordNum()/mqf.getOnePageNum());
else mqf.setPageCount(mqf.getRecordNum()/mqf.getOnePageNum()+1);
List showList=dao.getShowList(queryList,mqf.getPageCount(),mqf.getCurentPage(),mqf.getOnePageNum(),mqf.getRecordNum());
request.setAttribute("rs",showList);
return (actionMapping.findForward("view")); //此处的view转向pageAction.do
}
return null;
}
}
还用到了一个dao里的getShowList(queryList,mqf.getPageCount(),mqf.getCurentPage(),mqf.getOnePageNum(),mqf.getRecordNum())函数
//分页查询
public List getShowList(List queryList,int count,int page,int onepageNum,int total)
{
ArrayList rslist = new ArrayList();
if(count==page){
for(int i=(page-1)*onepageNum;i<total;i++){
rslist.add(queryList.get(i));
}
}
else{
for(int i=(page-1)*onepageNum;i<page*onepageNum;i++){
rslist.add(queryList.get(i));
}
}
return rslist;
}
4、配置文件 xml
<!--************** 关于分页 *************-->
<action type = "publicpackage.PageAction"
name = "pageActionForm"
scope = "request"
path = "/pageAction">
<forward name = "result" path = "/logmanager/loglist.jsp"/>
<forward name = "cautionpage" path = "/caution/viewcaution.jsp"/>
<forward name = "cueditpage" path = "/closeup.edit/editList.jsp"/>
<forward name = "woauditingpage" path = "/writeoff.auditing/auditingList.jsp"/>
<forward name = "wofellbackpage" path = "/writeoff.fellback/rejectList.jsp"/>
<forward name = "woprintpage" path = "/writeoff.print/printList.jsp"/>
<forward name = "woapprovepage" path = "/writeoff.approve/approvelist.jsp"/>
<forward name = "simplequerypage" path = "/query/simplelist.jsp"/>
</action>
5、Pageaction和Form
/*
* 创建日期 2005-12-12
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package publicpackage;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.*;
import javax.servlet.http.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class PageAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception{
PageActionForm paf = (PageActionForm)form;
List allResult = (List)request.getSession().getAttribute("pagelist");
String pageFlag = request.getSession().getAttribute("pageflag").toString();
paf.setRecordNum(allResult.size());
if(paf.getRecordNum()%paf.getOnePageNum()==0)
paf.setPageCount(paf.getRecordNum()/paf.getOnePageNum());
else paf.setPageCount(paf.getRecordNum()/paf.getOnePageNum()+1);
if(paf.getRecordNum()==0){
request.setAttribute("rs",new ArrayList());
return (mapping.findForward(this.getPageModel(pageFlag)));
}
if(paf.getCurentPage()==0)
paf.setCurentPage(1);
List showList=this.getShowList(allResult,paf.getPageCount(),paf.getCurentPage(),paf.getOnePageNum(),paf.getRecordNum());
request.setAttribute("rs",showList);
return (mapping.findForward(this.getPageModel(pageFlag)));
}
//分页功能
public List getShowList(List queryList,int count,int page,int onepageNum,int total){
ArrayList rslist = new ArrayList();
if(count==page){
for(int i=(page-1)*onepageNum;i<total;i++){
rslist.add(queryList.get(i));
}
}
else{
for(int i=(page-1)*onepageNum;i<page*onepageNum;i++){
rslist.add(queryList.get(i));
}
}
return rslist;
}
//得到需要分页的模块
private String getPageModel(String str){
if(str.equals("log"))
return "result";
else if(str.equals("caution"))
return "cautionpage";
else if(str.equals("woauditing"))
return "woauditingpage";
else if(str.equals("wofellback"))
return "wofellbackpage";
else if(str.equals("woapprove"))
return "woapprovepage";
else if(str.equals("woprint"))
return "woprintpage";
else if(str.equals("cuedit"))
return "cueditpage";
else if(str.equals("simplequery"))
return "simplequerypage";
else
return null;
}
}
========form=====
/*
* 创建日期 2005-12-12
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package publicpackage;
import org.apache.struts.action.*;
/**
* @author Administrator
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
public class PageActionForm extends ActionForm{
private int curentPage = 0;
private int pageCount = 0;
private int onePageNum = 15;
private int recordNum = 0;
/**
* @return 返回 curentPage。
*/
public int getCurentPage() {
return curentPage;
}
/**
* @param curentPage 要设置的 curentPage。
*/
public void setCurentPage(int curentPage) {
this.curentPage = curentPage;
}
/**
* @return 返回 onePageNum。
*/
public int getOnePageNum() {
return onePageNum;
}
/**
* @param onePageNum 要设置的 onePageNum。
*/
public void setOnePageNum(int onePageNum) {
this.onePageNum = onePageNum;
}
/**
* @return 返回 pageCount。
*/
public int getPageCount() {
return pageCount;
}
/**
* @param pageCount 要设置的 pageCount。
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
/**
* @return 返回 recordNum。
*/
public int getRecordNum() {
return recordNum;
}
/**
* @param recordNum 要设置的 recordNum。
*/
public void setRecordNum(int recordNum) {
this.recordNum = recordNum;
}
}