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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With