这个普通的分页没有用到任何前台的框架,因为我要做一个比较恶心的项目,必须让IE6支持,我试了一下其他的都不怎么好使,只能自己手动的写一个分页,这里只用功能,关于样式什么时候用到再稍微的修改修改吧。
首先来着大致的的图片:
接下来我们开始看它的代码
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-striped table-hover table-bordered">
<thead>
<tr align="center">
<th>序号</th>
<th>系统名称</th>
<th>所属单位</th>
</tr>
<tr>
</tr>
</thead>
<tbody>
**<s:iterator value="list" var="li">**
<tr class="success" align="center">
<td>年号</td>
<td><s:property value="#li.appid"/></td>
<td><s:property value="#li.menue"/></td>
</tr>
**</s:iterator>**
</tbody>
<分页部分>
**<tr ><td colspan="17" align="center">
<input type="hidden" id="perPagerRows" value="${pager.perPageRows}">//每次刷新的时候js给select做选中
//select的onchange是点击之后就提交,相当于前面加了一个a标签
<span>每页显示
<select id="selectOp" onchange="self.location.href=options[selectedIndex].value">
<option id="10" value="<%= basePath%>JRXTAction.action?pager.perPageRows=10">10</option>
<option id="20" value="<%= basePath%>JRXTAction.action?pager.perPageRows=20">20</option>
<option id="50" value="<%= basePath%>JRXTAction.action?pager.perPageRows=50">50</option>
<option id="100" value="<%= basePath%>JRXTAction.action?pager.perPageRows=100">100</option>
<option id="200" value="<%= basePath%>JRXTAction.action?pager.perPageRows=200">200</option>
<option id="1000" value="<%= basePath%>JRXTAction.action?pager.perPageRows=1000">1000</option>
</select>条记录
</span>
<s:if test="%{pager.curPage>1}">
<a href='JRXTAction.action?pager.curPage=1'>
<span >首页</span>
</a>
<a href='JRXTAction.action?pager.curPage=${pager.curPage-1 }'>
<span>上一页</span>
</a>
</s:if>
<span>共 ${pager.rowCount}记录,共${pager.curPage}/${pager.pageCount}页</span>
<s:if test="pager.curPage < pager.pageCount">
<a href="JRXTAction.action?pager.curPage=${pager.curPage+1}">
<span>下一页</span>
</a>
<a href='JRXTAction.action?pager.curPage=${pager.pageCount }'>
<span>尾页</span>
</a>
</s:if>
</td></tr></table>**
</div>
</div>
</div>
Pager部分
public class Pager {
private int curPage;//当前第几页
private int perPageRows ;//每页显示的记录数
private int rowCount; //记录数据总数
private int pageCount; //总页数
省略setter与getter方法
}
action部分(此处将sql语句也写一起)
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import DataCountView.JRXTEntity;
import com.hcycom.dao.JDBCUtil;
import com.opensymphony.xwork2.ActionSupport;
public class JRXTAction extends ActionSupport{
private static final long serialVersionUID = 1L;
private Pager pager;
private List<JRXTEntity> list;
//此处省略setter和getter方法
public Connection lj() {
try {
Connection conn = JDBCUtil.getConnection();
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public void close(ResultSet rs, Statement sql, Connection conn) {
JDBCUtil.closeResource(rs, sql, conn);
}
public String getJRXTEntity1(){
int curPage=1;
int perPageRows=10;
if(pager!=null){//这是防止刷新页面时候进来if语句,会出现只有pager一个属性有值,其他没有值会出现0的情况
System.out.println("pager not null");
if(pager.getCurPage()!=0){
curPage=pager.getCurPage();
}
if(pager.getPerPageRows()!=0){
perPageRows = pager.getPerPageRows();
}
}
System.out.println("pager.getPerPageRows():"+perPageRows+"cur:"+curPage);
int count=countJRXTEntity();//查出来数据总共条数
list = new ArrayList<JRXTEntity>();
try {
Connection conn1 = lj();
Statement sql1 = conn1.createStatement();
ResultSet rq = sql1.executeQuery("select * from rkrm$_log_taskinfo limit "+(curPage-1)*perPageRows+","+perPageRows+""); //limit后两个参数是第几条数据开始查,每页显示几条数据。
while (rq.next()) {
JRXTEntity jrxtEntity = new JRXTEntity();
jrxtEntity.setAppid(rq.getString(1));
jrxtEntity.setMenue(rq.getString(2));
jrxtEntity.setMenuid(rq.getString(3));
list.add(jrxtEntity);
}
close(rq, sql1, conn1);
Pager page=new Pager();
//设置pager对象中的perPageRows属性,表示每页显示的记录数
page.setPerPageRows(perPageRows);
//设置pager对象中的rowCount属性,表示记录总数
page.setRowCount(count);
page.setCurPage(curPage);
if(count%perPageRows!=0){//计算有几页
page.setPageCount(count/perPageRows+1);
}else {
page.setPageCount(count/perPageRows);
}
pager=page;//将查出来的值放在pager实体里面
} catch (SQLException e) {
e.printStackTrace();
}
return "aaaaa";
}
}