Struts2分页1
前段时间完成了一个项目,其中涉及到分页的功能,下面我将其拿出来与大家进行共享。
?
第一、是前台的jsp页面代码:
<s:if test="#request.hasprepage==false && #request.hasnextpage==true"><br/>
<%Page pages=(Page)request.getAttribute("pages");
for(int i=1;i<=pages.getTotalPage();i++){
%>
[<a href="curNewsPage.action?currentPage=<%=i %>"><%=i %></a>]
<%}%>
<a href="curNewsPage.action?currentPage=<s:property value="currentPage+1"/>">
?<img src="common/images/iepiao/next.gif" /></a>
</s:if>
<s:elseif test="#request.hasnextpage==false && #request.hasprepage==false"><br/>
<%Page pages=(Page)request.getAttribute("pages");
for(int i=1;i<=pages.getTotalPage();i++){
%>
[<a href="curNewsPage.action?currentPage=<%=i %>"><%=i %></a>]
<%}%>
</s:elseif>
<s:elseif test="#request.hasnextpage==true && #request.hasprepage==true"><br/>
<a href="curNewsPage.action?currentPage=<s:property value="currentPage-1"/>">
<img src="common/images/iepiao/pre.gif"/></a>
<%Page pages=(Page)request.getAttribute("pages");
for(int i=1;i<=pages.getTotalPage();i++){
%>
[<a href="curNewsPage.action?currentPage=<%=i %>"><%=i %></a>]
<%}%>
<a href="curNewsPage.action?currentPage=<s:property value="currentPage+1"/>">
<img src="common/images/iepiao/next.gif" /></a>??
</s:elseif>
<s:else>
<a href="curNewsPage.action?currentPage=<s:property value="currentPage-1"/>">
<img src="common/images/iepiao/pre.gif"/></a>
<%Page pages=(Page)request.getAttribute("pages");
for(int i=1;i<=pages.getTotalPage();i++){
%>
[<a href="curNewsPage.action?currentPage=<%=i %>"><%=i %></a>]
<%}%>
</s:else>
?
第二、action中的部分代码:
???? int numb = bulletinService.getTotalNumb();
???? if(currentPage==0){
???? ?currentPage=1;}
??????? Page pages=pageutil.createPage(16, numb, currentPage);
???? if(currentPage>pages.getTotalPage())
???? {
???? ?currentPage=pages.getTotalPage();
???? }
???? if(currentPage<1){
???? ?currentPage=1;
???? }
?????
?????pages=pageutil.createPage(16, numb, currentPage);
?????this.setList(bulletinService.findAllByPageStatusType("Bulletin", pages.getBeginIndex(), pages.getEveryPage(), "enteringdate", "desc"));
?????
???? Map request=(Map)ActionContext.getContext().get("request");??
???? request.put("hasprepage",pageutil.getHasPrePage(currentPage));
???? request.put("hasnextpage", pageutil.getHasNextPage(pages.getTotalPage(), currentPage));
???? request.put("pages",pages);
?
第三、service实现类代码:
???? public List findAllByPageStatusType(String table,int start,int limit,String orderby,String desc){
??????????? return this.bulletinDao.findAllByPageStatusType(table, start, limit, orderby, desc);
???? }
?
第四、分页工具类代码:
public class PageUtil {
?
?
?public static Page createPage(int everyPage,int totalCount,int currentPage) {
??everyPage = getEveryPage(everyPage);
??currentPage = getCurrentPage(currentPage);
??int totalPage = getTotalPage(everyPage, totalCount);
??int beginIndex = getBeginIndex(everyPage, currentPage);
??boolean hasPrePage = getHasPrePage(currentPage);
??boolean hasNextPage = getHasNextPage(totalPage, currentPage);
??return new Page(everyPage, totalCount, totalPage, currentPage,beginIndex, hasPrePage,? hasNextPage);
?}
?
?public static Page createPage(Page page,int totalCount) {
??int everyPage = getEveryPage(page.getEveryPage());
??int currentPage = getCurrentPage(page.getCurrentPage());
??int totalPage = getTotalPage(everyPage, totalCount);
??int beginIndex = getBeginIndex(everyPage, currentPage);
??boolean hasPrePage = getHasPrePage(currentPage);
??boolean hasNextPage = getHasNextPage(totalPage, currentPage);
??return new Page(everyPage, totalCount, totalPage, currentPage,beginIndex, hasPrePage,? hasNextPage);
?}
?
?public static int getEveryPage(int everyPage) {
??return everyPage == 0 ? 10 : everyPage;
?}
?
?public static int getCurrentPage(int currentPage) {
??return currentPage == 0 ? 1 : currentPage;
?}
?
?public static int getTotalPage(int everyPage,int totalCount) {
??int totalPage = 0;
??if(totalCount % everyPage == 0) {
???totalPage = totalCount / everyPage;
??} else {
???totalPage = totalCount / everyPage + 1;
??}
??return totalPage;
?}
?
?public static int getBeginIndex(int everyPage,int currentPage) {
??return (currentPage - 1) * everyPage;
?}
?
?public static boolean getHasPrePage(int currentPage) {
??return currentPage == 1 ? false : true;
?}
?
?public static boolean getHasNextPage(int totalPage, int currentPage) {
??return currentPage == totalPage || totalPage == 0 ? false : true;
?}
?
}
?
第五、分页基类代码:
public class Page {
?
?// 1.(everyPage)
?private int everyPage;
?// 2.(totalCount)
?private int totalCount;
?// 3.(totalPage)
?private int totalPage;
?// 4.(currentPage)
?private int currentPage;
?
?// 5.(beginIndex)
?private int beginIndex;
?// 6.(hasPrePage)
?private boolean hasPrePage;
?// 7.(hasNextPage)
?private boolean hasNextPage;
???????? public Page(int everyPage2, int totalCount2, int totalPage2,
???int currentPage2, int beginIndex2, boolean hasPrePage2,
???boolean hasNextPage2) {
??// TODO Auto-generated constructor stub
??????? ?
??????? ? this.everyPage=everyPage2;
??????? ? this.totalCount=totalCount2;
??????? ? this.totalPage=totalPage2;
??????? ? this.currentPage=currentPage2;
??????? ? this.beginIndex=beginIndex2;
??????? ? this.hasPrePage=hasPrePage2;
??????? ? this.hasNextPage=hasNextPage2;
?}
???????? public Page(){
??????? ?
???????? }
?public int getEveryPage() {
??return everyPage;
?}
?public void setEveryPage(int everyPage) {
??this.everyPage = everyPage;
?}
?public int getTotalCount() {
??return totalCount;
?}
?public void setTotalCount(int totalCount) {
??this.totalCount = totalCount;
?}
?public int getTotalPage() {
??return totalPage;
?}
?public void setTotalPage(int totalPage) {
??this.totalPage = totalPage;
?}
?public int getCurrentPage() {
??return currentPage;
?}
?public void setCurrentPage(int currentPage) {
??this.currentPage = currentPage;
?}
?public int getBeginIndex() {
??return beginIndex;
?}
?public void setBeginIndex(int beginIndex) {
??this.beginIndex = beginIndex;
?}
?public boolean isHasPrePage() {
??return hasPrePage;
?}
?public void setHasPrePage(boolean hasPrePage) {
??this.hasPrePage = hasPrePage;
?}
?public boolean isHasNextPage() {
??return hasNextPage;
?}
?public void setHasNextPage(boolean hasNextPage) {
??this.hasNextPage = hasNextPage;
?}
}
?
***不足之处还请各位多多指教***