I have been working on a company-wide PDF search using
Lucene and
PDFBox. Everything is being indexed nicely (tip, when debugging wierd search results with Lucene, try
Luke sooner instead of later, chances are you need to tune your index options) and I have a pretty html page where users can do plain text searches of all our PDFs or by category.
As hundreds to thousands of results can be returned, I want an easy way for users to page through the results. Traditionally with PHP or Perl I would parse http get options for the starting results and manually make some navigation code for this. I am lazy though, so I started searching for Java libraries to help make this easier. I hit the jackpot with the
Display tag library.
This library makes paging through data sets almost ludicrously easy. Once you've added the library to your development environment, write your bean to store a list version of your data. I used an ArrayList of a custom class to match the columns.
public class ResultSet {
private String column1;
private String column2;
public ResultSet (String column1, String column2) {
this.column1 = column1;
this.column2 = column2;
}
public String getColumn1() {
return column1;
}
public String getColumn2() {
return column2;
}
}
Then I added these results, compiled via my search action, to an ArrayList using add(new ResultSet("blah", "blah")); for each result and added a function to my bean to return this list. Then it was a simple matter of adding this code to my JSP (I use jspx for nearly everything, personal preference):
<jsp:scriptlet>
request.setAttribute( "resultList", hits.getListSet() );
</jsp:scriptlet>
<display:table name="resultList" pagesize="15" requestURI="index.jspx">
<display:setProperty name="paging.banner.placement" value="both" />
<display:column property="column1" title="Column 1" escapeXml="true" />
<display:column property="column2" title="Column 2" escapeXml="true" />
</display:table>And bam, instant paging support. I didn't have to code any navigation code at all. Just theme it via css using the provided span classes and you are done. It also adds odd and even classes to the table rows so you can easily do fancy alternating row colors for readability.