admin管理员组

文章数量:1434943

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>

I am populating a table in my jsp which has more than 20 records. Now, If I want to display each 10 records in a page and then the user clicks on next link to go to the other 20 records.

How can I do that in my jsp?

This is my current code

<table>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>type</td>
    </tr>
<%
    while(resultSet.next())
    {
        %>
        <tr>
        <td><%=resultSet.getString(1)%></td>
        <td><%=resultSet.getString(2)%></td>
        <td><%=resultSet.getString(3)%></td>
    </tr>
<%
    }
%>
</table>
Share Improve this question asked Aug 2, 2010 at 16:04 maasmaas 1,2235 gold badges14 silver badges13 bronze badges 1
  • 1 Duplicate of ResultSet to Pagination. Just a warning: you really don't want to do this in "raw" Java code in JSP. It'll on long term only give trouble in all colors. – BalusC Commented Aug 2, 2010 at 16:47
Add a ment  | 

3 Answers 3

Reset to default 1

You can use 'display' tag which provides pagination, sorting, exporting. In your current code, you are iterating a result set and creating table rows. Having Java code in JSP is not the best practice. Move that into a servlet and create a collection of beans by iterating the result set and set the collection in request scope or session scope(If you want to avoid multiple DB hits).

Sample Usage (From display tag site)

<display:table name="sessionScope.test" pagesize="10">
    <display:column property="id" title="ID" />
    <display:column property="name" />
    <display:column property="email" />
    <display:column property="status" />
</display:table>

Here the 'test' is the name of the attribute that stores a collection. The 'property' attributes for column tag represent the properties of your bean contained in the collection. 'pagesize' specifies the pagination size that the number of items to be displayed in a page.

For more info: http://displaytag.sourceforge/1.2/index.html

I know that you are looking for a back-end solution, but how about making it easier for the user (by not forcing page refresh on each request) and have a js solution (a js pagination solution)?

A list of jQuery pagination solutions

  • List of jQuery pagination plugins
  • 15 more jQuery pagination plugins
  • Another one (includes pagination + sorting)
  • Another 20 Table pagination plugins

Again, this is only a suggestion, go for whatever works for you (and your users) best.

As requested: see a good beginner tutorial or Another one.

What you want is a pagination code example. Here's one I found online: http://www.easywayserver./blog/jsp-pagination/ There are lots of examples of this and you'll have to find which one formats your nav links the way you like them

本文标签: javascriptHow to limit the HTML table records in each pageStack Overflow