10 examples of displaytag in JSP, Struts and Spring (original) (raw)
Display tag is one of the best free open source libraries for showing data in tabular format in a J2EE application using jsp, struts, or spring. it is shipped as a tag library so you can just include this in your jsp and add the corresponding jar and their dependency in class-path and you are ready to use it. The display tag is my favorite solution for displaying any tabular data because of its inherent capability in paging and sorting. It provides great support for pagination on its own but it also allows you to implement your own pagination solution. On the Sorting front, you can sort your data based on any column, implement default sorting, etc.
There is so much resource available on using display tags including examples, demos, and guides. In this article, we will see some important points to note while using display tags in jsp.
Though this library is very stable and rich in functionality still there are some subtle things that matter and if you don't know you could potentially waste hours to fix those things. These are the points I found out while I was using displaytag in my project and I have listed those down here for benefit of all.
displaytag examples
I have outlined all the examples based upon task I had to perform and due to those tasks, I discovered and get myself familiar with display tag. It’s my personal opinion that task-based approach works better in terms of understanding something than feature based. It’s simply easy for the mind to understand the problem first and then a solution.
1. Provide UID while using two tables in one page.
In many scenarios, we need to show two separate tables in one jsp page. We can do this by using two tags easily but the catch is that sorting will not work as expected. When you sort one table, the second table will automatically sort or vice-versa.
To make two tables independent of each other for functionality like exporting and sorting you need to provide "uid" attribute to table tag as shown below. I accidentally found this when I encounter a sorting issue on the display tag.
<displaytag:table name="listofStocks" id="current_row" export="true" uid="1"> <displaytag:table name="listofStockExchanges" id="current_row" export="true" uid="2">
just make sure "uid" should be different for each table.
2. Displaytag paging request starts with "d-"
There was a bug in one of our jsp which shows data using displatag, with each paging request it was reloading data which was making it slow. Then we thought to filter out displaytag paging requests and upon looking the pattern of displaytag paging requests we found that it always starts with "d-", so by using this information you can filter out display tags paging request. Since we were using spring it was even easier as shown in below
Example:
Map stockParamMap = WebUtils.getParametersStartingWith(request, "d-"); if(stockParamMap.size() !=0){ out.println("This request is displaytag pagination request"); }
3. Getting a reference to the current row in display tag
Many times we require a reference of the current row while rendering display tag data into a jsp page. In our case we need to get something from currentRow and then get Something from another Map whose key was value retrieved from current row, to implement this, of course, we somehow need a reference of the current row in display tag.
After looking online and displaytag.org we found that by using "id" attribute we can make current row reference available on pageScope. Name of the variable would be the value of "id" attribute, this would be much clear by seeing below example:
<displaytag:table name="pennyStocks" id="current_penny_stock" export="true" uid="1"> <di:column title="Stock Price" value="${pennyStockPrice[current_penny_stock.RIC]}" sortable="true" />
This way we are displaying stock price from pennyStockPrice Map whose key was RIC(Reuters Information Code) of penny Stock. You see name of variable used to refer current row is "current_penny_stock".
4. Formatting date in displaytag in JSP
Formatting date and numbers are extremely easy in display tag, you just need to specify a "format" attribute with displaytag:column tag and value of this tag would be date format as we used in SimpleDateFormat in Java. In Below example, I am showing date in "yyyy-MM-dd" format.
<di:column title="Stock Settlement Date" property="settlementDate" sortable="true" format="{0,date,yyyy-MM-dd}" />
You can also implement your own table decorator or column decorator but I found this option easy and ready to use.
5. Sorting Columns in display tag
Again very easy you just need to specify sortable="true" with displaytag:column tag and display tag will make that column sortable. Here is an example of a sortable column in display tag.
<di:column title="Stock Price" property="stockPrice" sortable="true" />
6. Making a column link and passing its value as a request parameter.
Sometimes we need to make the value of a particular column as the link may be to show another set of data related to that value. we can do this easily in display tags by using "href" attribute of displaytag:column tag, value of this attribute should be a path to target URL.
If you want to pass value as request parameter you can do that by using another attribute called "paramId", which would become name of request parameter.
Here is an example of making a link and passing the value as the request parameter in display tags.
<di:column property="stockSymbol" sortable="true" href="details.jsp" paramId="symbol"/>
Values on this column will appear as link and when user click on the link it will append a request parameter "symbol" e.g symbol=Sony
7. Default sorting and ordering in displaytag
If you want that your table data is by default sorted based upon a particular column when displayed then you need to define a column name for default sorting and an order e.g. ascending or descending.
You can achieve this by using attributes "defaultsort" and "defaultorder" of displaytag:table tag as shown in the below example.
<displaytag:table name="pennyStocks" id="current_penny_stock" export="true" defaultsort="1" defaultorder="descending" uid="1">
This will display a table which would be sorted on the first column in descending order.
8. Sorting whole list data as compared to only page data in display tag
This was the issue we found once we have done with our displaytag jsp. we found that whenever we sort the table by clicking on any sortable column header it only sorts the data visible on that page, it was not sorting the whole list provided to display tag I mean data that was on other pages was left out. That was not the desirable action for us.
Upon looking around we found that the display tag by default sorts only current page data but you can override this behavior by providing a displaytag.properties file in classpath and including the below line in
displaytag.properties: sort.amount = list
9. Configuring displaytag by using displaytag.properties
This was an important piece of information that we are not aware until we hit by above-mentioned the issue. Later we found that we can use displaytag.properties to customize different behaviors, the appearance of displaytag.
Another good behavior we discovered was showing an empty table if provided list is null or empty. You can achieve this by adding line "basic.empty.showtable = true". Here was how our properties file looks like
sort.amount = list basic.empty.showtable = true basic.msg.empty_list=No results matched your criteria. paging.banner.placement=top
10. Specifying pagesize for paging in a JSP
You can specify how many rows you want to show in one page by using "pagesize" attribute of displaytag:table. We prefer to use it from configuration because this was subject to change.
Overall we found that displaytag was quite rich in functionality, easy to use and highly configurable. Displaytag is also very extensible in terms of customizing export or pagination functionality. Displaytag.org has some very good examples and live demo addressing each of the display tag functionality and those are an excellent starting point as well. No matter whether you are using struts, spring or plain servlet based framework for tabular data displaytag is a good choice.
Note: In displaytag:table displaytag is tag prefix I used in while declaring tag library using <@taglib> in JSP.
Related Java Tutorials