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.
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.
01.public class ActorData
02.{
03.private String tvShow;
04.private String userName;
05.private String emailId;
06.public ActorData(String tvShow, String userName, String emailId)
07.{
08.this.tvShow = tvShow;
09.this.userName = userName;
10.this.emailId = emailId;
11.}
12.public ArrayList loadData()
13.{
14.ArrayList userList = new ArrayList();
15.userList.add(new ActorData("The Office","Michael Scott",
16."michael.scott@dundermifflin.com"));
17.userList.add(new ActorData("The Office","Dwight Schrute",
18."dwight.schrute@dundermifflin.com"));
19.userList.add(new ActorData("The Office","Jim Halpert",
20."jim.halpert@dundermifflin.com"));
21.userList.add(new ActorData("The Office","Pam Beesly",
22."pam.beesly@dundermifflin.com"));
23.userList.add(new ActorData("The Office","Andy Bernad",
24."andy.bernad@dundermifflin.com"));
25.userList.add(new ActorData("The Office","Angela Martin",
26."angela.martin@dundermifflin.com"));
27.userList.add(new ActorData("Friends","Rachel Green",
28."rachel.green@friends.com"));
29.userList.add(new ActorData("Friends","Monica Geller",
30."monica.geller@friends.com"));
31.userList.add(new ActorData("Friends","Phoebe Buffay",
32."phoebe.buffay@friends.com"));
33.userList.add(new ActorData("Friends","Joey Tribbiani",
34."joey.tribbiani@friends.com"));
35.userList.add(new ActorData("Friends","Chandler Bing",
36."chandler.bing@friends.com"));
37.userList.add(new ActorData("Friends","Ross Geller",
38."ross.geller@friends.com"));
39.return userList;
40.}
41.public String getTvShow() {
42.return tvShow;
43.}
44.public String getUserName() {
45.return userName;
46.}
47.public String getEmailId() {
48.return emailId;
49.}
50.
51.}
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.
1.public ActionForward
execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response) throwsException {
2.UserForm userForm = (UserForm) form;
3.ActorData actorData = new ActorData();
4.userForm.setActorList(actorData.loadData());
5.return mapping.findForward(SUCCESS);
6.}
To create dynamic links for the email
ids, we will create a decorator class which extendsTableDecorator. The decorator
attribute of the table tag should hold the path of the decorator class.
1.<display:table id="data" name="sessionScope.UserForm.actorList"requestURI="/userAction.do" pagesize="6"decorator="com.vaannila.ActorDecorator">
2.<display:column property="tvShow" title="TV Show" />
3.<display:column property="userName" title="User Name" />
4.<display:column property="emailId" title="Email Id" />
5.</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.
01.public class ActorDecorator extends TableDecorator {
02.
03.public String getTvShow()
04.{
05.ActorData actorData =
(ActorData)getCurrentRowObject();
06.return actorData.getTvShow();
07.}
08.
09.public String getUserName()
10.{
11.ActorData actorData =
(ActorData)getCurrentRowObject();
12.return actorData.getUserName();
13.}
14.
15.public String getEmailId()
16.{
17.ActorData actorData =
(ActorData)getCurrentRowObject();
18.String emailId = "<a
href=\"mailto:"+actorData.getEmailId()+"\">"+actorData.getEmailId()+"</a>";
19.return emailId;
20.}
21.
22.}
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.
No comments:
Post a Comment