Hibernate 分頁技術(下拉框)

public class ViewContent1 extends Action{
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response)
   throws Exception {
  ItemContentService ns = (ItemContentService) AppFactory.instance().getApp("contentService");
  List pageList=null;//查詢結果
  int currentPage=1;//當前的頁面
  int total=0;//總頁數
  int pageLength = 4;//每頁顯示的記錄數

  String p=request.getParameter("page");
  if(p!=null)
      currentPage=Integer.parseInt(p);

   try{
   total=ns.getTotalRowByContent("公司介紹");//根據"公司介紹"查詢到的結果集的
   }catch(Exception e){
    e.getMessage();
   }
   
   if(total>0){
    Page page=new Page(total,pageLength,currentPage);
    pageList=ns.getContentByContent("公司介紹",page.getPageStartRow(), page.getPageLength());//查詢到的結果集
      
    if(pageList.size()>0){
        request.setAttribute("pageList",pageList);
        request.setAttribute("totalRows",page.getTotalRows());
     request.setAttribute("hasNextPage",page.isHasNextPage());
     request.setAttribute("hasPreviousPage",page.isHasPreviousPage());
     request.setAttribute("totalPages",page.getTotalPages());
     request.setAttribute("currentPage",page.getCurrentPage());
     request.setAttribute("page","不爲空");
       }else{
        request.setAttribute("page","記錄爲空");
        request.setAttribute("nullContent","記錄爲空");
       }
   }
  
  return mapping.findForward("succ");
 }
}
 

# viewContent1.jsp #
<logic:match value="記錄爲空" name="page" scope="request">
     <tr align="center">
  <bean:write name="nullContent" scope="request"/>     
  </tr>
    </logic:match>

    <tr><td bgcolor="#7E9BE5" align="center" colspan="5"><strong><a href="#" οnclick="del();">刪除選中的欄目</a></strong></td></tr>
   
    <form name="pagechange" action="viewContent1.do" method="post">
      <tr align="center"><td colspan="6">共<bean:write name="totalRows" scope="request"/>條記錄
        &nbsp;&nbsp;<bean:write name="currentPage" scope="request"/>/<bean:write name="totalPages" scope="request"/>&nbsp;&nbsp;
          <logic:present name="hasPreviousPage" scope="request">
       <logic:equal name="hasPreviousPage" value="true">
     <a href="viewContent1.do?page=${requestScope.currentPage-1}">上一頁</a>&nbsp;&nbsp;
      </logic:equal>
       </logic:present>
          <logic:present name="hasNextPage" scope="request">
      <logic:equal name="hasNextPage" value="true">
     <a href="viewContent1.do?page=${requestScope.currentPage+1}">下一頁</a>
      </logic:equal>
       </logic:present>
        &nbsp;&nbsp;跳到
          <select name="page" οnchange="javascript:document.pagechange.submit();">
           <%
              int totalPages=((Integer)request.getAttribute("totalPages")).intValue();
              for(int i=0;i<totalPages;i++){
           %>
            <option value="<%=i+1%>"<%if(i+1==((Integer)request.getAttribute("currentPage")).intValue()){%>selected<%}%>><%=i+1%></option>
           <%}%>
           </select>頁
       </td></tr>
    </form>


public class Page {
 
 private int currentPage=1;
 private int totalRows=0;//總記錄數
 private int pageStartRow=0;//開始記錄位置
 private int pageLength=10;//頁記錄長度
 private int totalPages=0;//總頁數
 private boolean hasPreviousPage=false; //是否有上一頁
 private boolean hasNextPage=false; //是否有下一頁
 
 public Page() {
  
 }

 public Page(int totalRows, int pageLength, int cPage) {
  this.currentPage=cPage;
  this.totalRows=totalRows;
  this.pageLength=pageLength;
  init();
 }
 
 private void init(){
  if(totalRows%pageLength==0)
   totalPages=totalRows/pageLength;
  else
   totalPages=totalRows/pageLength+1;

  if(currentPage>totalPages)
   currentPage=totalPages;
  else if(currentPage<1)
   currentPage=1;

  if(currentPage<totalPages)
  {
   hasNextPage=true;
  }else{
   hasNextPage=false;
  }

  if(currentPage>1)
  {
   hasPreviousPage=true;
  }else{
   hasPreviousPage=false;
  }

  pageStartRow=(currentPage-1)*pageLength;
 }
 
 public int getPageLength() {
  return pageLength;
 }


 public int getPageStartRow() {
  return pageStartRow;
 }

 public boolean isHasNextPage() {
  return hasNextPage;
 }

 public void setHasNextPage(boolean hasNextPage) {
  this.hasNextPage = hasNextPage;
 }

 public boolean isHasPreviousPage() {
  return hasPreviousPage;
 }

 public void setHasPreviousPage(boolean hasPreviousPage) {
  this.hasPreviousPage = hasPreviousPage;
 }

 public int getTotalPages() {
  return totalPages;
 }

 public void setTotalPages(int totalPages) {
  this.totalPages = totalPages;
 }

 public int getTotalRows() {
  return totalRows;
 }

 public void setTotalRows(int totalRows) {
  this.totalRows = totalRows;
 }

 public void setPageLength(int pageLength) {
  this.pageLength = pageLength;
 }

 public void setPageStartRow(int pageStartRow) {
  this.pageStartRow = pageStartRow;
 }

 public int getCurrentPage() {
  return currentPage;
 }

 public void setCurrentPage(int currentPage) {
  this.currentPage = currentPage;
 }

}

 

# struts-config.xml #
<action path="/admin/viewContent1" type="com.cst.web.background.introduction.ViewContent1"  scope="request" >
            <forward name="succ" path="/WEB-INF/jsp/back/dynamic/viewContent1.jsp" />
        </action>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章