3 Replies Latest reply: Jun 30, 2011 12:59 AM by 872365 RSS

    jsp paging

    871439
      I have a relatively simple problem but I can't find out the solution.
      I have an array what includes numbers. I would like to list 10 elements/page. But if the actual page is the second I can't open the third. Only the previous link works.

      NumbersBean
      public class NumbersBean {
      
          List<Integer> numbers;
          int actPage;
          int maxPage = 10;
          HtmlDataTable dataTable;
          String pagingDirection;
      
          public NumbersBean() {
          }
      
          public List<Integer> getNumbers() {
              numbers = new LinkedList<Integer>();
              for (int i = 0; i < 100; i++) {
                  numbers.add(i);
              }
              int from = actPage * maxPage;
              int to = from + maxPage;
              if (to >= numbers.size()) {
                  to = numbers.size();
              }
              return numbers.subList(from, to);
          }
      
          public void setNumbers(List numbers) {
              this.numbers = numbers;
          }
      
          protected void addMessage(String message) {
              FacesMessage msg = new FacesMessage(message);
              FacesContext.getCurrentInstance().addMessage(null, msg);
          }
      
          public int getActPage() {
              return actPage;
          }
      
          protected int calculatePageCount() {
              return (numbers.size() / maxPage) + ((numbers.size() % maxPage) != 0 ? 1 : 0);
          }
      
          public int getPageCount() {
              return calculatePageCount();
          }
      
          public String getPagingDirection() {
              return pagingDirection;
          }
      
          public void setPagingDirection(String pagingDirection) {
              this.pagingDirection = pagingDirection;
          }
      
          public String page() {
      
              if (pagingDirection.equals("prev") && actPage != 0) {
                  actPage--;
              } else if (pagingDirection.equals("next") && actPage + 1 < calculatePageCount()) {
                  actPage++;
              }
              return null;
          }
      
          public HtmlDataTable getDataTable() {
              return dataTable;
          }
      
          public void setDataTable(HtmlDataTable dataTable) {
              this.dataTable = dataTable;
          }
      }
      main.jsp
      <html>
          <head>
              <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
              <title>Numbers</title>
          </head>
          <body>
              <f:view>
                  <h:form>
                      <h:dataTable value="#{NumbersBean.numbers}" var="numbers"
                                   styleClass="table" rowClasses="row1, row2"
                                   binding="#{NumbersBean.dataTable}">
                          <h:column>
                              <h:outputText value="#{numbers}" />
                          </h:column>
                      </h:dataTable>
                      <h:commandLink value="< " action="#{NumbersBean.page}" styleClass="pad"
                                     rendered="#{NumbersBean.actPage>0}">
                          <f:setPropertyActionListener value="prev" target="#{NumbersBean.pagingDirection}" />
                      </h:commandLink>
                      <h:commandLink value=" >" action="#{NumbersBean.page}" styleClass="pad"
                                     rendered="#{NumbersBean.actPage+1<NumbersBean.pageCount}">
                          <f:setPropertyActionListener value="next" target="#{NumbersBean.pagingDirection}" />
                      </h:commandLink>
                      <h:messages />
                  </h:form>
              </f:view>
          </body>
      </html>
        • 1. Re: jsp paging
          EJP
          public List<Integer> getNumbers() {
          numbers = new LinkedList<Integer>();
          for (int i = 0; i < 100; i++) {
          numbers.add(i);
          }
          int from = actPage * maxPage;
          int to = from + maxPage;
          if (to >= numbers.size()) {
          to = numbers.size();
          }
          return numbers.subList(from, to);
          }
          Off-topic, but that's a bizarre way of returning the numbers frrom 'from' to 'to'. Just run the loop from 'from' to 'to' inclusive and get rid of all the other rubbish.
          protected int calculatePageCount() {
          return (numbers.size() / maxPage) + ((numbers.size() % maxPage) != 0 ? 1 : 0);
          }
          return (numbers.size()+maxPage-1) / maxPage;
          • 2. Re: jsp paging
            871439
            This is only a sample code, but thank you for your advices. Although I don't understand why I can't navigate to the third page.
            • 3. Re: jsp paging
              872365
              ==> if (pagingDirection.equals("next") && actPage + 1 < calculatePageCount())
              s/b if (pagingDirection.equals("next") && actPage + 1 <= calculatePageCount())