Note that there are some explanatory texts on larger screens.

plurals
  1. POImplement pagination in JSP page in a Spring MVC and Hibernate application
    text
    copied!<p>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.</p> <p>Here is what I did until now:</p> <pre><code>&lt;div class="container"&gt; &lt;table class="table table-hover"&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;Age&lt;/th&gt; &lt;c:forEach items="${employees}" var="emp"&gt; &lt;tr&gt; &lt;td&gt;${emp.firstName}&lt;/td&gt; &lt;td&gt;${emp.lastName}&lt;/td&gt; &lt;td&gt;${emp.Age}&lt;/td&gt; &lt;/tr&gt; &lt;/c:forEach&gt; &lt;/table&gt; &lt;div class="pagination"&gt; &lt;ul&gt; &lt;li class="disabled"&gt;&lt;a href="#"&gt;First&lt;/a&gt;&lt;/li&gt; &lt;li class="disabled"&gt;&lt;a href="#"&gt;Prev&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;1&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;2&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;3&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;4&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;5&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;Next&lt;/a&gt;&lt;/li&gt; &lt;li class="active"&gt;&lt;a href="#"&gt;Last&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/div&gt; &lt;/div&gt; </code></pre> <p>This is the related code in my controller:</p> <pre><code>@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"; } </code></pre> <p>This is the related code in my Service class:</p> <pre><code>public List&lt;Employee&gt; getEmployees(int page) { return employeeDao.getEmployeeList(page); } </code></pre> <p>This is the related code in my DAO class:</p> <pre><code>private static final int limitResultsPerPage = 3; public List&lt;Employee&gt; getEmployeeList(int page) { Query q = sessionFactory.getCurrentSession().createQuery( "from Employee"); q.setFirstResult(page * limitResultsPerPage); q.setMaxResults(limitResultsPerPage); return (List&lt;Employee&gt;) q.list(); } </code></pre> <p>The page where I am displaying that table is list.jsp</p> <p>Here are my assumptions of what I should do but dont know how (please correct me if I am taking the wrong way):</p> <p>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.</p> <p>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.</p> <p>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?</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload