Implement pagination in JSP page in a Spring MVC and Hibernate application

25,593

To answer one of your questions at least:

You can pass the page number and other parameters from the JSP to your controller with the RequestParam annotation like this:

  @RequestMapping(value = "/list", method = RequestMethod.GET)
  public String getEmployees(@RequestParam(value = "page", required = false) Long page, ModelMap model) {
        //now page is available.
    model.addAttribute("employees", this.employeeService.getEmployees(page));
    return "listing";
  }

And your link will look something like this:

  list/?page=1

Pagination is a fairly complicated process but here are a few ideas. You can use JSTL on the JSP page to implement the logic. For instance:

    <c:if test="${page > 1}">
       <li class="active"><a href="#">First</a></li>
    </c:if>

I suggest that you do some calculations in the Action for the number of pages that you want to display. Say for instance that you always want to display ten links. On page 1, you will display pages 1...10, on page 7 you will display pages 2...12 and so on. In the action you can determine the starting page and the ending page to display.

      int startpage = page - 5 > 0?page - 5:1;
      int endpage = startpage + 10;

On the JSP page you can do a loop maybe:

    <c:forEach begin="${startpage}" end="${endpage}" var="p">
         <a href="#">${p}</a>
    </c:forEach>

and so on.

Share:
25,593
dukable
Author by

dukable

Updated on July 29, 2022

Comments

  • dukable
    dukable almost 2 years

    I am trying to implement pagination in my JSP page. I am using Spring MVC and Hibernate. The java code part is okay but I am having difficulties implementing it in my JSP page. I am using twitter bootstrap.

    Here is what I did until now:

    <div class="container">
        <table class="table table-hover">
            <th>First Name</th>
            <th>Last Name</th>
            <th>Age</th>
            <c:forEach items="${employees}" var="emp">
            <tr>
                <td>${emp.firstName}</td>
                <td>${emp.lastName}</td>
                <td>${emp.Age}</td>
            </tr>
            </c:forEach>
        </table>
    
        <div class="pagination">
            <ul>
                <li class="disabled"><a href="#">First</a></li>
                <li class="disabled"><a href="#">Prev</a></li>
                <li class="active"><a href="#">1</a></li>
                <li class="active"><a href="#">2</a></li>
                <li class="active"><a href="#">3</a></li>
                <li class="active"><a href="#">4</a></li>
                <li class="active"><a href="#">5</a></li>
                <li class="active"><a href="#">Next</a></li>
                <li class="active"><a href="#">Last</a></li>
            </ul>
        </div>
    </div>
    

    This is the related code in my controller:

    @RequestMapping(value = "/list", method = RequestMethod.GET)
        public String getEmployees(ModelMap model) {
                **//I believe I should get the page number from the JSP here but how?**
            model.addAttribute("employees", this.employeeService.getEmployees(page));
            return "listing";
    }
    

    This is the related code in my Service class:

    public List<Employee> getEmployees(int page) {
            return employeeDao.getEmployeeList(page);
    }
    

    This is the related code in my DAO class:

    private static final int limitResultsPerPage = 3;
    
    public List<Employee> getEmployeeList(int page) {
            Query q = sessionFactory.getCurrentSession().createQuery(
                    "from Employee");
            q.setFirstResult(page * limitResultsPerPage); 
            q.setMaxResults(limitResultsPerPage);
            return (List<Employee>) q.list();
    }
    

    The page where I am displaying that table is list.jsp

    Here are my assumptions of what I should do but dont know how (please correct me if I am taking the wrong way):

    Modify my link that point to list.jsp in my menu, to list.jsp?page=0, so everytime the user clicks on the link, he will arrive in the first page.

    When the user clicks one of the button, I need to pass the page number to my controller so I can manage to return the right "employees" with my query.

    As you can see, the First and Previous buttons are deactivated as I am currently on the first page. So my question is, how should I handle the activation/deactivation of those buttons, as well as the Next and Last ones?

    Also, how to "refresh" the numbers on the list? For example, if the user is at the page 20, I won't display buttons from 1 to 19?

  • dukable
    dukable over 11 years
    Thank you, I did follow your advice, no issue :) Do you know what is the best practice to manage the First, Previous, Next and Last buttons?
  • dukable
    dukable over 11 years
    Thanks for adding some more details Vincent :) Last question, the name of my link, that I want to be the number of the current page using ${page} is not interpreted, I am probably missing something: <li><a href="<c:url value="/listing.htm" ><c:param name="page" value="${page}"/>${page}</c:url>"></a></li>