Struts中实现分页功能(转载)
1.建立页数类public class PageBean
{
private int curPage = 1; //当前页
private int totalPages; //总页数
private int rowsPerPage = 5; //每页显示行数
private int totalRows; //总行数
private String order = "all"; //排序实际没用,当有检测排序的时候有用
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public int getRowsPerPage() {
return rowsPerPage;
}
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public String getOrder() {
return order;
}
public void setOrder(String order) {
this.order = order;
}
}
2.BIZ
//根据条件查出宠物信息
public ArrayList findPetInfoByParamer( PetInfo petInfo,PageBean page )
{
ArrayList list = new ArrayList();
String sql1 = "";
String sql2 = "";
String sql3 = "";
String sql4 = "";
String sql5 = "";
String sql6 = "";
String sql7 = "";
String name = petInfo.getPet_name();
if(name!=null && !name.equals(""))
{
sql1 = "and pet_name like '%"+name+"%'";
sql5 = sql5+sql1;
}
int type = petInfo.getPet_type();
if( type!=-1 ){
sql2 = "and pet_type=" + type;
sql5 = sql5+sql2;
}
String owner = petInfo.getPet_owner_name();
if( owner!=null && !owner.equals("") ){
sql3 = "and pet_owner_name like '%" + owner + "%'";
sql5 = sql5+sql3;
}
//排序
String order = page.getOrder();
if(!order.equals("all") )
{
sql7 =" order by " + order + " desc";
}
else
{
sql7 =" order by (pet_strength+pet_cute+pet_love) desc";
}
//not in 后的查询
sql4 = "select top " + (page.getCurPage()-1)*page.getRowsPerPage() + " pet_id from PetInfo where 1=1 ";
sql4 = sql4+sql5+sql7;
//对查询结果进行分页
sql6 = "select top " + page.getRowsPerPage() + " * from PetInfo where pet_id not in("
+ sql4 + ") " + sql5;
if(!order.equals("all") )
{
sql6 = sql6 + " order by " + order + " desc";
}
else
{
sql6 = sql6 + " order by (pet_strength+pet_cute+pet_love) desc";
}
System.out.println("sql=" + sql6);
try
{
conn = super.getConnection();
pst = conn.prepareStatement(sql6);
rs = pst.executeQuery();
while(rs.next())
{
PetInfo pet = new PetInfo();
pet.setPet_id(rs.getInt(1));
pet.setPet_name(rs.getString(2));
pet.setPet_sex(rs.getString(3));
pet.setPet_strength(rs.getInt(4));
pet.setPet_cute(rs.getInt(5));
pet.setPet_love(rs.getInt(6));
pet.setPet_intro(rs.getString(7));
pet.setPet_owner_name(rs.getString(8));
pet.setPet_owner_email(rs.getString(9));
pet.setPet_password(rs.getString(10));
pet.setPet_pic(rs.getString(11));
pet.setPet_type(rs.getInt(12));
list.add(pet);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
super.closeAll(conn, pst, rs);
}
return list;
}
//根据条件查询总行数
public int findTotalRows( PetInfo petInfo,PageBean page ){
int totalRows = 0;
String sql1 = "";
String sql2 = "";
String sql3 = "";
String sql4 = "";
String sql5 = "";
String name = petInfo.getPet_name();
if( name!=null && !name.equals("") ){
sql1 = "and pet_name like '%" + name + "%'";
sql5 = sql5+sql1;
}
int type = petInfo.getPet_type();
if( type!=-1 ){
sql2 = "and pet_type=" + type;
sql5 = sql5+sql2;
}
String owner = petInfo.getPet_owner_name();
if( owner!=null && !owner.equals("") ){
sql3 = "and pet_owner_name like '%" + owner + "%'";
sql5 = sql5+sql3;
}
sql4 = "select count(*) pet_id from PetInfo where 1=1 ";
sql4 = sql4+sql5;
//执行SQL语句
try
{
conn = super.getConnection();
pst = conn.prepareStatement(sql4);
rs = pst.executeQuery();
if(rs.next())
{
totalRows = rs.getInt(1);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
super.closeAll(conn, pst, rs);
}
return totalRows;
}
//根据条件查询总页数
public int findTotalPages( PetInfo petInfo,PageBean page ){
int totalPages = 0;
int totalRows = 0;
totalRows = this.findTotalRows(petInfo, page);
if( totalRows%page.getRowsPerPage()==0 ){
totalPages = totalRows / page.getRowsPerPage();
} else {
totalPages = totalRows / page.getRowsPerPage() +1;
}
return totalPages;
}
3.Action
//查找宠物
public ActionForward toGetPets(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
{
PetInfoForm petInfoForm = (PetInfoForm) form;
if(request.getParameter("name")!=null)
{
petInfoForm.getPet().setPet_name(request.getParameter("name"));
}
System.out.println(request.getParameter("what"));
if(request.getParameter("what")==null)
{
}
else
{
petInfoForm.getPet().setPet_name("");
petInfoForm.getPet().setPet_type(-1);
petInfoForm.getPet().setPet_owner_name("");
}
HttpSession session = request.getSession();
PetInfoBiz biz = new PetInfoBiz();
//后面这才是有用的
ArrayList list = biz.findPetInfoByParamer(petInfoForm.getPet(), petInfoForm.getPage());
session.setAttribute("allpet",list);
int totalPages = biz.findTotalPages(petInfoForm.getPet(),petInfoForm.getPage());
int totalRows = biz.findTotalRows(petInfoForm.getPet(),petInfoForm.getPage());
petInfoForm.getPage().setTotalPages(totalPages);
petInfoForm.getPage().setTotalRows(totalRows);
return mapping.findForward("petlist");
}
4.显示页面
<div>
<div class="input_title">总积分排名</div>
<table class="data_table">
<tr>
<th width="40px">序号</th>
<th width="200px">宠物名</th>
<th width="50px">类别</th>
<th width="50px">总积分</th>
<th width="40px">力量</th>
<th width="40px">聪明</th>
<th width="40px">爱心</th>
<th width="150px">主人</th>
</tr>
<logic:iterate id="pet" name="allpet" indexId="i">
<tr>
<td style="text-align:right;">
${i+1+(petInfoForm.page.curPage-1)*(petInfoForm.page.rowsPerPage)}
</td>
<td>
<a href="petInfo.do?method=toLookPet&id=${pet.pet_id}" target="_blank">
${pet.pet_name }
</a>
</td>
<logic:equal value="1" name="pet" property="pet_type">
<td style="text-align:right;">千禧猪</td>
</logic:equal>
<logic:equal value="2" name="pet" property="pet_type">
<td style="text-align:right;">猫咪</td>
</logic:equal>
<logic:equal value="3" name="pet" property="pet_type">
<td style="text-align:right;">哥斯拉</td>
</logic:equal>
<td style="text-align:right;">
${pet.pet_strength+pet.pet_cute+pet.pet_love }
</td>
<td style="text-align:right;">${pet.pet_strength}</td>
<td style="text-align:right;">${pet.pet_cute}</td>
<td style="text-align:right;">${pet.pet_love}</td>
<td style="text-align:center;"><a href="mailto:${pet.pet_owner_email }">${pet.pet_owner_name }</a></td>
</tr>
</logic:iterate>
</table>
共${petInfoForm.page.totalRows }条记录
每页显示<input name="page.rowsPerPage" value="${petInfoForm.page.rowsPerPage }" size="3" />条
第<input name="page.curPage" value="${petInfoForm.page.curPage }" size="3" />页
/ 共${petInfoForm.page.totalPages }页
<a href="javascript:page_first();">第一页</a>
<a href="javascript:page_pre();">上一页</a>
<a href="javascript:page_next();">下一页</a>
<a href="javascript:page_last();">最后一页</a>
<button onclick="javascript:page_go();">GO</button>
<script>
function page_go()
{
page_validate();
document.forms[0].submit();
}
function page_first()
{
document.forms[0].elements["page.curPage"].value = 1;
document.forms[0].submit();
}
function page_pre()
{
var pageNo = document.forms[0].elements["page.curPage"].value;
document.forms[0].elements["page.curPage"].value = parseInt(pageNo) - 1;
page_validate();
document.forms[0].submit();
}
function page_next()
{
var pageNo = document.forms[0].elements["page.curPage"].value;
document.forms[0].elements["page.curPage"].value = parseInt(pageNo) + 1;
page_validate();
document.forms[0].submit();
}
function page_last()
{
document.forms[0].elements["page.curPage"].value = ${petInfoForm.page.totalPages };
document.forms[0].submit();
}
function page_validate()
{
var pageNo = document.forms[0].elements["page.curPage"].value;
if (pageNo<1)
pageNo=1;
if (pageNo>${petInfoForm.page.totalPages})
pageNo=${petInfoForm.page.totalPages};
document.forms[0].elements["page.curPage"].value = pageNo;
}
</script></div>