DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Data Engineering
  3. Data
  4. Displaytag TableDecorator Tutorial

Displaytag TableDecorator Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Tutorial
Like (0)
Save
Tweet
Share
58.73K Views

Join the DZone community and get the full member experience.

Join For Free

A "decorator" is a design pattern where one object provides the functionality to decorate an other object. Its a better to have all the formatting code seperately in a decorator class instead of having it along with the business logic. The decorator can also be used to provide dynamic links based on the value of each property. In this example we will see how to create dynamic links using decorators in display tag.

The following jar files should be placed in the WEB-INF/lib directory

  • antlr
  • commons-beanutils
  • commons-beanutils-1.7.0
  • commons-collections-3.1
  • commons-digester
  • commons-fileupload-1.0
  • commons-lang-2.3
  • commons-logging
  • commons-validator
  • displaytag-1.2
  • displaytag-export-poi-1.2
  • displaytag-portlet-1.2
  • itext-1.3
  • jakarta-oro
  • log4j-1.2.13
  • struts

The following taglib directive should be placed in each JSP page that uses the display taglib.

<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

In this example we will display a list of actor's details like name, email Id and the TV show in which they performed. Our ActorData class stores the actor's details like name, email id and the tv show. The ActorData class has a loadData() method which returns an ArrayList of all the actor details.

The following attributes and methods are present in the ActorData class.

public class ActorData
{
    private String tvShow;
    private String userName;
    private String emailId;
    public ActorData(String tvShow, String userName, String emailId)
    {
        this.tvShow = tvShow;
        this.userName = userName;
        this.emailId = emailId;
    }
    public ArrayList loadData()
    {
        ArrayList userList = new ArrayList();
        userList.add(new ActorData("The Office","Michael Scott",
        "michael.scott@dundermifflin.com"));
        userList.add(new ActorData("The Office","Dwight Schrute",
        "dwight.schrute@dundermifflin.com"));
        userList.add(new ActorData("The Office","Jim Halpert",
        "jim.halpert@dundermifflin.com"));
        userList.add(new ActorData("The Office","Pam Beesly",
        "pam.beesly@dundermifflin.com"));
        userList.add(new ActorData("The Office","Andy Bernad",
        "andy.bernad@dundermifflin.com"));
        userList.add(new ActorData("The Office","Angela Martin",
        "angela.martin@dundermifflin.com"));
        userList.add(new ActorData("Friends","Rachel Green",
        "rachel.green@friends.com"));
        userList.add(new ActorData("Friends","Monica Geller",
        "monica.geller@friends.com"));
        userList.add(new ActorData("Friends","Phoebe Buffay",
        "phoebe.buffay@friends.com"));
        userList.add(new ActorData("Friends","Joey Tribbiani",
        "joey.tribbiani@friends.com"));
        userList.add(new ActorData("Friends","Chandler Bing",
        "chandler.bing@friends.com"));
        userList.add(new ActorData("Friends","Ross Geller",
        "ross.geller@friends.com"));
        return userList;
    }
    public String getTvShow() {
        return tvShow;
    }
    public String getUserName() {
        return userName;
    }
    public String getEmailId() {
        return emailId;
    }

}

Inside the execute() method in UserAction, the loadData() method of ActorData class is called. This method will return an ArrayList of actors, that ArrayList is stored in the actorList attribute of the UserForm class.

public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception {
    UserForm userForm = (UserForm) form;
    ActorData actorData = new ActorData();
    userForm.setActorList(actorData.loadData());
    return mapping.findForward(SUCCESS);
}

To create dynamic links for the email ids, we will create a decorator class which extends TableDecorator. The decorator attribute of the table tag should hold the path of the decorator class.

<display:table id="data" name="sessionScope.UserForm.actorList" requestURI="/userAction.do" pagesize="6" decorator="com.vaannila.ActorDecorator">
    <display:column property="tvShow" title="TV Show" />
    <display:column property="userName" title="User Name" />
    <display:column property="emailId" title="Email Id" />
</display:table>

In the decorator class the getCurrentRowObject() method returns an instance of the object present in the list. Our actorList in the form contains a list of ActorData, so the object returned by the getCurrentRowObject() method will be of type ActorData. We first need to typecaste the object to ActorData and then we can access the attributes using the getter and setter methods.

public class ActorDecorator extends TableDecorator {

    public String getTvShow()
    {
        ActorData actorData = (ActorData)getCurrentRowObject();
        return actorData.getTvShow();
    }

    public String getUserName()
    {
        ActorData actorData = (ActorData)getCurrentRowObject();
        return actorData.getUserName();
    }

    public String getEmailId()
    {
        ActorData actorData = (ActorData)getCurrentRowObject();
        String emailId = "<a href=\"mailto:"+actorData.getEmailId()+"\">"+actorData.getEmailId()+ "</a>";
        return emailId;
    }

}

The getter methods for the properties in the ActorData class should be specified in the ActorDecorator class. If specified the getter method present in the ActorDecorator class will be invoked, if not the one in the ActorData class will be invoked. The getTvShow() and getUserName() methods need not be specified here since they are just returning the property values. If you want to do any formatting before displaying the data, then it can be done in the corresponding getter method of that property in the decorator class.

On clicking the email id a mail will be triggered to the corresponding person. This is achieved by using the <a> tag. In this way dynamic links can be added to data grid based on the property values.

The data gid with the dynamic links for the email id is displayed below.

On clicking the email id a mail will be sent to the corresponding user.

You can download the source code of the Displaytag TableDecorator example by clicking on the Download link below.

Source: Download

Source + Lib: Download 

Links Property (programming) Object (computer science) Attribute (computing) Download Data grid Data (computing) Data structure

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Use Golang for Data Processing With Amazon Kinesis and AWS Lambda
  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • Multi-Tenant Architecture for a SaaS Application on AWS
  • Uber System Design

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: