2

I'm trying to use JSTL to create a pagination in my existing JSP code but I am not sure about the following syntax which I've seen in one of the examples.

    <sql:setDataSource var="dataSrc"
     url="jdbc:oracle:thin:@127.0.0.1:1521:database_name"
     driver="oracle.jdbc.driver.OracleDriver"
     user="user_name" password="pass_word"/>

Then you run a query:

    <sql:query var="queryResults" dataSource="${dataSrc}">
    select system_id, employeename from employees
    </sql:query>

Then, you display the results on the web page:

    <table>
    <tr>
    <th>ID</th>....

My question is <sql: query var="queryResults" is this a standard syntax I must use or what do I put inside this query var= ? and also the dataSource ="{dataSrc}" is this a standard code or must I modify this?

If someone can direct me to a source example of how to display my tables or rows limited to 10 on each page I'd be very grateful.

Bala R
  • 101,930
  • 22
  • 186
  • 204
Karen Goh
  • 124
  • 1
  • 5
  • 19

2 Answers2

2

I've written my own pagination tag before - I really wish I hadn't.

I would suggest you take a look at displaytag. It's an easy to use open source tag library that should pretty much cover all your pagination requirements. The HTML it produces is clean and standards compliant and best of all, it's already been written :-)

For proper pagination, usage can be as simple as this:

<display:table name="${paginatedList}" partialList="true" pageSize="10" size="${totalNumberOfItems}" />

${paginatedList} is e.g. items 1 to 10 of your resultset (you'll need to write the code in your DAO to retrieve items from your database in chunks. Most ORM libraries allow you to do this pretty easily, or you can do it with SQL - see below for examples).

${totalNumberOfItems} is the total number of items your query would return if you didn't limit the number of results to a page size of 10. Again, most ORM frameworks allow you to do this fairly easily, or again you can do it with SQL (e.g. select count(*)...) - see below...

For an example of pagination in Hibernate, take a look at this. For a JDBC example, have a look at this.

Community
  • 1
  • 1
rcgeorge23
  • 3,234
  • 3
  • 23
  • 47
  • I've tried the displaytag that you have mentioned but it's just not working for me. I've put in the displaytag under the lib folder and then have all the right coding but it's just not working. Not sure if it's because I'm not using any framework. Any way I can learn basic and the simplest framework in java? – Karen Goh May 29 '12 at 16:28
0

With a little bit of extra knowledge you could have used Struts 2.0 pagination

JSP code spinet from below reference guide

<pg:pager
items="<%= searchResultPagerSize %>"
index="center"
url=""
maxPageItems="<%= numberOfItemsPerPage %>"
maxIndexPages="<%= maxNumberOfPagesToShow %>"
isOffset="<%= true %>"
export="offset,currentPageNumber=pageNumber"
scope="request">

<%-- START: the visual element of the pager --%>
<%--
START: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<pg:index export="totalItems=itemCount">
    <pg:page export="firstItem, lastItem">
        <div class="resultInfo">
            Displaying results <strong><%= firstItem%>-<%= lastItem%></strong> of <strong><%= totalItems%></strong> found
        </div>
    </pg:page>

    <div class="rnav">
        <span class="rnavLabel">Results:</span> 
        <pg:prev export="pageUrl">
            <a href="<%= pageUrl%>" class="rnavLink">« Prev</a> 
        </pg:prev>
        <pg:pages export="pageUrl,pageNumber,firstItem,lastItem">
            <% if (pageNumber == currentPageNumber) {%>
             <span class="rnavCurr"><%= firstItem%>-<%= lastItem%></span>
            <% } else {%>
             <a href="<%= pageUrl%>" class="rnavLink"><%= firstItem%>-<%= lastItem%></a>
            <% }%>
        </pg:pages>
        <pg:next export="pageUrl">
              <a href="<%= pageUrl%>" class="rnavLink">Next »</a>
        </pg:next>
    </div>
</pg:index>
<%--
END: You can take any of the pages in the pagertags war file in the folder /WEB-INF/JSP and put them in here instead
--%>
<%-- END: the visual element of the pager --%>

see here for the complete reference. I think this is what you exactly need.

Switch
  • 12,929
  • 19
  • 66
  • 108