实现SSI分页的基本步骤:查看使用数据库的分页方式;java后台数据的处理,jsp前台数据的显示
采用mysql数据库分页,主要用到mysql数据检索中的limit a,b 函数实现,a表示从第几条开始,b表示需要获取的条数,采用ibatis框架xml代码如下:
select ... from table limit #start#,#pageSize#
java后台数据的处理主要采用自己写一个分页类来实现前台和数据的交互操作,代码如下:
package com.common.pojo;
import java.io.Serializable;
public class PageInfo implements Serializable {
private static final long serialVersionUID = 7553973735794350843L;
/**
* 总记录数 默认为-1
*/
private int totalRecords = -1;
/**
* 总页数
*/
private int totalPages;
/**
* 每页显示的记录数
* */
private int pageSize = 10;
/**
* 当前所在页码(在第几页)
* */
private int curPageNo = 1;
/**
* 显示第几页
*/
private int curPage;
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurPageNo() {
return curPageNo;
}
public void setCurPageNo(int curPageNo) {
this.curPageNo = curPageNo;
}
public int getTotalPages() {
this.totalPages=this.totalRecords%this.pageSize==0?this.totalRecords/this.pageSize:this.totalRecords/this.pageSize+1;
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getTotalRecords() {
return totalRecords;
}
public void setTotalRecords(int totalRecords) {
this.totalRecords = totalRecords;
}
}
定义了几个分页的基本属性,可根据自己进行优化。初始化了属性的值,将pageSize默认一页10条,getTotalPage直接将现有的数据计算出来,方便调用。
在BaseDomain将pageInfo作为它的属性来调用,然后每个不同的domain继承BaseDomain
public class BaseDomain implements Serializable {
private static final long serialVersionUID = -653238472375867369L;
PageInfo page = null;
public PageInfo getPage() {
if(page == null){
page = new PageInfo();
}
return page;
}
public void setPage(PageInfo page) {
this.page = page;
}
}
后台dao层代码:
(List<ExamInfoDomain>)queryForList("query_exam_info",map);
service层,根据页面传过来的数据设置Map
Map<String, Object> map = new HashMap<String, Object>();
map.put("start", (page.getCurPageNo()-1)*page.getPageSize());
map.put("pageSize",page.getPageSize());
......
action层直接调用service层方法就可以了。
页面实现分页:
<s:form action="/queryExamByCondition" id="mainForm" namespace="/" method="post">
<s:hidden name="examInfoDomain.page.totalPages"></s:hidden>
.....
<div class="pth_page">
<a href="javascript:queryBefOrNext(${examInfoDomain.page.curPageNo-1 <= 1?1:examInfoDomain.page.curPageNo-1})" class="nbtn">上一页</a>
<s:if test="examInfoDomain.page.curPageNo <= examInfoDomain.page.totalPages / 7 * 7">
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="1 + 7 * ((examInfoDomain.page.curPageNo-1)/7)" />
<s:param name="last" value="7 + 7 * ((examInfoDomain.page.curPageNo-1)/7)" />
<s:iterator var="curPage">
<s:if test="#curPage == examInfoDomain.page.curPageNo">
<a href="javascript:GotoPage(<s:property/>);" class="current"> <s:property/></a>
</s:if>
<s:else>
<a href="javascript:GotoPage(<s:property/>);"> <s:property/></a>
</s:else>
</s:iterator>
</s:bean>
</s:if>
<s:else>
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="examInfoDomain.page.totalPages / 7 * 7 + 1" />
<s:param name="last" value="examInfoDomain.page.totalPages" />
<s:iterator var="curPage">
<s:if test="#curPage == examInfoDomain.page.curPageNo">
<a href="javascript:GotoPage(<s:property/>);" class="current"> <s:property/></a>
</s:if>
<s:else>
<a href="javascript:GotoPage(<s:property/>);"> <s:property/></a>
</s:else>
</s:iterator>
</s:bean>
</s:else>
<a href="javascript:queryBefOrNext(${examInfoDomain.page.curPageNo+1 >= examInfoDomain.page.totalPages?examInfoDomain.page.totalPages:examInfoDomain.page.curPageNo+1})" class="nbtn">下一页</a>
共${examInfoDomain.page.totalPages}页 到第<input type="text" id="curPageNo" value="" class="cinput">页
<a href="javascript:GotoPage()" class="cbtn">确定</a>
</div>
...
</form>
javascript 实现方法:
function GotoPage(pageNum){
if(pageNum == undefined || pageNum == null || $.trim(pageNum) == ""){
pageNum = $("#curPageNo").val();
var reg = /^\d+$/;
if(reg.test(pageNum)==false){
return;
}
var totalPages = $("#mainForm_examInfoDomain_page_totalPages").val();
if( $.trim(pageNum) * 1 > totalPages * 1)
{
pageNum = totalPages;
}
}
var form = $("#mainForm");
form.attr("action","queryExamByCondition?examInfoDomain.page.curPage="+$.trim(pageNum));
form.submit();
}
function queryBefOrNext(curPage){
var form = $("#mainForm");
form.attr("action","queryExamByCondition?examInfoDomain.page.curPage="+curPage);
form.submit();
}
这样,基于mysql数据库的struts2分页就搞定了,后台不难,前台页面实现1,2,3,4,5,6,7这个稍微有点不好弄。