做WEB方面免不了分页技术,我最近想研究研究,谁有空帮我写个简单的分页,ssh的或jsp的分页都行。
请把您的项目直接发到[email protected]邮箱中,谢谢。别忘了,在这里发条信息,好给您分呀。
请把您的项目直接发到[email protected]邮箱中,谢谢。别忘了,在这里发条信息,好给您分呀。
解决方案 »
- hibernate保存实例违反唯一约束条件 (VMSUSER.PK_ID),可表中没有这个约束啊
- struts Action代码分析
- JSP 调用 Linux shell写文件, 然后读取文件的问题!
- 用CMP 的 findAll 方法提取大数据量时报错,难道不能这么用吗?
- 急!问个EJB的问题 有难度~~
- 这句E文翻译啊?
- SSH框架的异常处理疑问。
- weblogic pool问题,请大家帮忙?谢谢!!!
- 那里有sybase驱动下载啊?
- JSP中想做一个视频播放功能,会的进来
- hibernate生成的abstraceModel和Model和ModelDAO,不想修改,在此条件下如何分层?
- 新手的困惑,老鸟都是怎么处理的
非常简单
与Struts结合使用最出名的一个tag主要是显示表格数据很漂亮、完善。
如ipager3.0
还有display tag
只要在lib下加上相关的jar,视图层页面加上3行标签就完事
这儿有详细的解释
然后函数查询的时候把它带上.List<Department> deps = db.from(Department.class).where(criteria, params).getPagedList(pageInfo);public class PageInfo implements Serializable { private int currentPage = 0; private int rowsPerPage = 20; private long total = -1;
/**
* @param rows
* @param rowsPerPage
* @param currentPage
*/
public PageInfo() {
} public PageInfo(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
} /**
* @return Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
} /**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} /**
* @return Returns the rowsPerPage.
*/
public int getRowsPerPage() {
return rowsPerPage;
} /**
* @param rowsPerPage
* The rowsPerPage to set.
*/
public void setRowsPerPage(int rowsPerPage) {
this.rowsPerPage = rowsPerPage;
} /**
* @return Returns the total.
*/
public long getTotal() {
return total;
} /**
* @param total
* The total to set.
*/
public void setTotal(long total) {
this.total = total;
}
/**
* @return Returns the total.
*/
public int getStartIndex() {
return currentPage * rowsPerPage;
} /**
* @return Returns the total.
*/
public long getLastIndex() {
int lastIndex = (currentPage + 1) * rowsPerPage - 1;
return (lastIndex >= total ? total - 1 : lastIndex);
} /**
*
*/
public String toString() {
return currentPage + "_" + rowsPerPage;
} /**
*
* @return
*/
public boolean getHasPrev() {
return currentPage > 0;
} /**
*
* @return
*/
public boolean getHasNext() {
return (currentPage + 1) * rowsPerPage < total;
} /**
*
* @return
*/
public PageInfo getPrev() {
if (currentPage > 0) {
PageInfo prev = new PageInfo();
prev.currentPage = currentPage - 1;
prev.total = total;
prev.rowsPerPage = rowsPerPage;
return prev;
} else {
return this;
}
} /**
*
* @return
*/
public PageInfo getNext() {
if ((currentPage + 1) * rowsPerPage < total) {
PageInfo next = new PageInfo();
next.currentPage = currentPage + 1;
next.total = total;
next.rowsPerPage = rowsPerPage;
return next;
} else {
return this;
}
}
/**
*
* @return
*/
public int getTotalPageNum() {
double p = (double)total / rowsPerPage;
return (int) p + (p - (int)p > 0 ? 1:0);
} /**
*
* @param str
* @param rowsPerPage
* @return
*/
public static PageInfo valueOf(String str) {
PageInfo pageInfo = new PageInfo();
if (str == null || str.length() == 0) {
pageInfo.currentPage = 0;
pageInfo.rowsPerPage = 15;
} else {
try {
int n = str.indexOf('_');
if (n > 0) {
pageInfo.currentPage = Integer.parseInt(str.substring(0, n));
pageInfo.rowsPerPage = Integer.parseInt(str.substring(n + 1));
} else {
pageInfo.currentPage = 0;
pageInfo.rowsPerPage = Integer.parseInt(str);
}
} catch (NumberFormatException e) {
}
}
return pageInfo;
}
}
在模板里调用这个PageInfo来写分页,给你一个Freeer宏的例子:
<#macro page_info>
<div style="float:right">
<span class="paging">
<#assign min=pageInfo.currentPage-4>
<#if (min < 0) >
<#assign min=0>
</#if>
<#if (min > 0) >
<span class="pageno"><a href="${request.requestURL}?page=0">1</a></span>
</#if>
<#assign max=pageInfo.currentPage-1>
<#if (max < 0) >
<#assign max=0>
</#if>
<#if (max>4)>
...
</#if>
<#if (pageInfo.currentPage>0)>
<#list min..max as p >
<span class="pageno"><a href="${request.requestURL}?page=${p}">${p+1}</a></span>
</#list>
</#if>
<span class="pageno on">
<a href="${request.requestURL}?page=${pageInfo.currentPage}">${pageInfo.currentPage+1}</a>
</span>
<#assign min=pageInfo.currentPage+1>
<#if (min > pageInfo.totalPageNum -1) >
<#assign min=pageInfo.totalPageNum - 1>
</#if>
<#assign max=pageInfo.currentPage+4>
<#if (max > pageInfo.totalPageNum-1) >
<#assign max=pageInfo.totalPageNum-1>
</#if>
<#if (pageInfo.currentPage<pageInfo.totalPageNum-1)>
<#list min..max as p >
<span class="pageno"><a href="${request.requestURL}?page=${p}">${p+1}</a></span>
</#list>
</#if>
<#if (max<pageInfo.totalPageNum-2)>
...
</#if>
<#if (max<pageInfo.totalPageNum-1)>
<span class="pageno"><a href="${request.requestURL}?page=${pageInfo.totalPageNum-1}">${pageInfo.totalPageNum}</a></span>
</#if>
<span class="matter">(${pageInfo.total}件)</span>
<span class="prev">
<#if pageInfo.hasPrev>
<a href="${request.requestURL}?page=${pageInfo.prev.currentPage}">前へ</a>
<#else>
前へ
</#if></span> |
<span class="next">
<#if pageInfo.hasNext>
<a href="${request.requestURL}?page=${pageInfo.next.currentPage}">次へ</a>
<#else>
次へ
</#if>
</span>
</span>
</div>
</#macro>
这代码看不懂。
回复 runffer_yang:
帮我整理整理,发个项目过来多好呀。
public List<FoodBean> GetFoods(int page,int count) {
// TODO Auto-generated method stub
int start = 0;
if(page!=1)
start = (page-1)*count;
String sql = "select top "+count +" * from foodInfo where foodId not in(select top "+ start +" foodId from foodInfo" + ")";
ResultSet rs = BaseDao.getResultSet(sql);
List<FoodBean> list = new ArrayList<FoodBean>();
try {
while(rs.next()){
FoodBean food = new FoodBean();
food.setDescription(rs.getString("description"));
food.setFoodId(rs.getString("foodId"));
food.setFoodImage(rs.getString("foodImage"));
food.setFoodName(rs.getString("foodName"));
food.setFoodPrice(rs.getDouble("foodPrice"));
food.setRe(rs.getString("re"));
list.add(food);
}
BaseDao.closeResultSet(rs);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
} public int getPageCount(int count) {
// TODO Auto-generated method stub
String sql = "select count(1) from foodinfo";
Connection conn=BaseDao.GetConnection();
int page = 0;
try {
PreparedStatement psmt = conn.prepareStatement(sql);
ResultSet rs = psmt.executeQuery();
if(rs.next())
page = rs.getInt(1);
rs.close();
BaseDao.closeStaement(psmt);
BaseDao.closeConnection(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(page/count==0) page = 1;
if(page/count>0&&page%count==0) page = page/count;
if(page/count>0&&page%count!=0) page = page/count + 1;
return page;
}
//pageSize 每页记录数
//resultSize 总记录数
public static int getMaxPage(int pageSize,int resultSize)
{
return (resultSize+pageSize-1)/pageSize;
}
//url 请求的链接
//pageIndex 当前索引
// maxPage 最大页数
public static String getPageNavigation(String url,int pageIndex,int maxPage)
{
StringBuilder sb=new StringBuilder("");
sb.append("");
sb.append("第"+(pageIndex+1)+"/"+maxPage+"页");
if(pageIndex>0)
{
sb.append("<a href='"+url+"?pageIndex=0'>首页</a>");
sb.append("<a href='"+url+"?pageIndex="+(pageIndex-1)+"'>上一页</a>");
}
else
{
sb.append("首页");
sb.append("上一页");
}
if(pageIndex<maxPage-1)
{
sb.append("<a href='"+url+"?pageIndex="+(pageIndex+1)+"'>下一页</a>");
sb.append("<a href='"+url+"?pageIndex="+(maxPage-1)+"'>尾页</a>");
}
else
{
sb.append("下一页");
sb.append("尾页");
}
sb.append("");
return sb.toString();
}}