2013-01-14 70 views
6

我想在我的JSP頁面中實現分頁。我正在使用Spring MVC和Hibernate。 java代碼部分沒問題,但我在JSP頁面中實現它時遇到困難。我正在使用twitter bootstrap。在Spring MVC和Hibernate應用程序的JSP頁面中實現分頁

這裏是我做的到現在爲止:

<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> 

這是在我的控制器相關的代碼:

@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"; 
} 

這是我在服務類中的相關代碼:

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

這是我的DAO類中的相關代碼:

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(); 
} 

我在哪裏顯示該表的頁面是

的List.jsp

這裏是我的我應該做的假設,但不知道如何(請糾正我,如果我採取了錯誤的方式):

在我的菜單中將指向list.jsp的鏈接修改爲list.jsp?page = 0,因此每次用戶點擊鏈接時,他都會到達第一頁。

當用戶點擊其中一個按鈕時,我需要將頁碼傳遞給我的控制器,這樣我就可以設法用我的查詢返回正確的「員工」。

正如您所看到的,第一個和上一個按鈕已停用,因爲我當前處於第一頁。所以我的問題是,我應該如何處理這些按鈕的激活/停用,以及Next和Last?

此外,如何「刷新」列表上的數字?例如,如果用戶在第20頁,我將不顯示1到19的按鈕?

回答

6

要回答至少您的問題之一:

您可以通過從JSP頁面數等參數,您的控制器與RequestParam註釋是這樣的:

@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"; 
    } 

而且你的鏈接會是這個樣子:

list/?page=1 

分頁是一個相當複雜的過程,但這裏有一些想法。您可以在JSP頁面上使用JSTL來實現邏輯。例如:

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

我建議您在Action中對您想要顯示的頁數進行一些計算。舉例來說,你總是想顯示10個鏈接。在第1頁上,您將顯示第1 ... 10頁,第7頁上您將顯示第2 ... 12頁等等。在該操作中,您可以確定要顯示的起始頁面和結束頁面。

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

在JSP頁面中,你可以做,也許一個循環:

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

等。

+0

謝謝你,我也跟着你的意見,沒有問題:)你知道管理First,Previous,Next和Last按鈕的最佳做法是什麼? – dukable

+0

感謝多加入一些細節文森特:)最後一個問題,我的鏈接的名字,我想用$ {PAGE}不解釋當前頁面的數量,我可能失去了一些東西:

  • ${page}">
  • dukable

    0

    文森特Ramdhanie的解決方案是正確的:感謝。 Dukable:我使用了基於文森特Ramdhanie的解決方案的代碼和它的作品真的很好:這樣的事情應該在你的代碼工作。

    @RequestMapping(value = "/list", method = RequestMethod.GET) 
    public String getEmployees(@RequestParam(value = "page", required = false) Long page, ModelMap model) { 
        //now page is available. 
    
        int startpage = (int) (page - 5 > 0?page - 5:1); 
        int endpage = startpage + 10; 
    
        model.addAttribute("employees", this.employeeService.getEmployees(page)); 
    
        model.addAttribute("startpage",startpage); 
        model.addAttribute("endpage",endpage); 
    
        return "listing"; 
    } 
    

    而在你的jsp:

    <div class="pagination"> 
        <ul> 
         <li><c:forEach begin="${startpage}" end="${endpage}" var="p"><a href="<c:url value="/list" ><c:param name="page" value="${p}"/>${p}</c:url>">${p}</a></c:forEach></li> 
        </ul> 
    </div> 
    

    您可以訪問到你的JSP在這條路上:

    http://localhost:8080/Project/list/?page=1