Saturday, April 11, 2020

10 examples of displaytag in JSP, Struts and Spring

Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Guides, Oracle Java Prep

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 tag library so you can just include this  in your jsp and add corresponding jar and there dependency in class-path and you are ready to use it. Display tag is my favorite solution for displaying any tabular data because of its inherent capability on paging and sorting. It provides great support for pagination on its own but it also allows you to implement your own pagination solution. On Sorting front you can sort your data based on any column, implement default sorting etc.

There is so much resource available on using display tag including examples, demos, and guides. In this article, we will see some important points to note while using display tag in jsp. Though this library is very stable and rich in functionality still there are some subtle things which 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 benefits 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 sorting issue on 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 request and upon looking the pattern of displaytag paging request 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 reference to current row in display tag


Many times we require reference of 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 some how 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 in pageScope. Name of 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.

Oracle Java Tutorial and Material, Oracle Java Learning, Oracle Java Guides, Oracle Java Prep

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.


Some time we need to make a particular columns value as 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 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"/>

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 defaine a column name for default sorting and an order e.g. ascending or descending. You can achieve this by using attribute "defaultsort" and "defaultorder" of <displaytag:table> tag as shown in below example.

<displaytag:table name="pennyStocks" id="current_penny_stock" export="true" defaultsort="1" defaultorder="descending" uid="1">

This will display 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 done with our displaytag jsp. we found that whenever we sort the table by clicking on any sortable column header it only sort the data visible on that page, it was not sorting the whole list provided to display tag I mean data which was on other pages was left out. That was not the desirable action for us. Upon looking around we found that display tag by default sort only current page data but you can override this behavior by providing a displaytag.properties file in classpath and including below line in

displaytag.properties:
sort.amount = list

9) Configuring displaytag by using displaytag.properties


This was an important piece of information which 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, appearence of displaytag. Another good behavior we discoved was showing 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

//displaytag.properties
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.

Related Posts

0 comments:

Post a Comment