Adding multiple parameters in the request using displaytag

I was struggled a lot and facing difficulties to add multiple parameters in the request while the displaytag option. Finally, able to find some useful links about it after a long struggle. Hence, decided to write a blog with all the possibilities together in detailed with samples.

In this blog, we have used mysql database to store and retrieve the data in the application.  Database Table structure is as follows:

Employee:

Id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
FirstName VARCHAR(20)
MiddleName  VARCHAR(20)
LastName VARCHAR(20)
Adress VARCHAR(50)
City VARCHAR(30)

Upon a successful creation of a table with the above structure, inserted some sample values into the database.

Upon a successful access of this table from the application tier, saved the data into List as follows:

while(rs.next()) {
   if(list == null) {
      list = new ArrayList<EmployeesResultBean>();
   }
   list.add(new EmployeesResultBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6)));
}
request.setAttribute( "result", list );

EmployeesResultBean class is as follows:

public class EmployeesResultBean{
int id;
String firstName, lastName, middleName, address, city;

/**
* @return the id
*/
   public int getId() {
      return id;
   }

/**
* @param id the id to set
*/
   public void setId(int id) {
      this.id = id;
   }

/**
* @return the firstName
*/
   public String getFirstName() {
      return firstName;
   }

/**
* @param firstName the firstName to set
*/
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }

/**
* @return the lastName
*/
   public String getLastName() {
      return lastName;
   }

/**
* @param lastName the lastName to set
*/
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }

/**
* @return the middleName
*/
   public String getMiddleName() {
      return middleName;
   }

/**
* @param middleName the middleName to set
*/
   public void setMiddleName(String middleName) {
      this.middleName = middleName;
   }

/**
* @return the address
*/
   public String getAddress() {
      return address;
   }

/**
* @param address the address to set
*/
   public void setAddress(String address) {
      this.address = address;
   }

/**
* @return the city
*/
   public String getCity() {
      return city;
   }

/**
* @param city the city to set
*/
   public void setCity(String city) {
      this.city = city;
   }

/**
* @param id
* @param firstName
* @param lastNamae
* @param middleName
* @param address
* @param city
*/
   public EmployeesBean(int id, String firstName, String lastName,String middleName, String address, String city) {
      super();
      this.id = id;
      this.firstName = firstName;
      this.lastName = lastName;
      this.middleName = middleName;
      this.address = address;
      this.city = city;
   }
}

Now, the result has been stored in the list and placed in the  request attribute to access further.

Following are the various options to display the result using displaytag options:
Basic table:

<display:table name="result" />

Basic, columns:


<display:table name="result">
<display:column property="id" title="ID" />
<display:column property="firstName" title="First Name"/>
<display:column property="middleName" title="Middle Name"/>
<display:column property="lastName"  title="Last Name"/>
<display:column property="address" title="Address"/>
<display:column property="city" title="City"/>
</display:table>

Sending the value as a parameter:

<display:table name="result">
<display:column property="id" title="ID" href="nextpage.jsp" paramId="id" paramProperty="id"/>
<display:column property="firstName" title="First Name"/>
<display:column property="middleName" title="Middle Name"/>
<display:column property="lastName"  title="Last Name"/>
<display:column property="address" title="Address"/>
<display:column property="city" title="City"/>
</display:table>

The hyperlink will point to ‘http://<$IP>:<$PORT>/<$CONTEXT>/nextpage.jsp?id=<$ID>’

Sending multiple parameters for another page:

To implement this, we need to use a decorator that can be used to create dynamic links on the fly so that you can either click on a particular column value and “drill down” for more information, or you can create a column of text labels which are hyperlinks that perform some action on the object in that row. These dynamic links can be created based on some primary key of the object, or they can make use of the object List index.

<display:table name="result" pagesize="15" decorator="com.samples.LinkDecorator">
<display:column property="link" title="ID" />
<display:column property="firstName" title="First Name"/>
<display:column property="middleName" title="Middle Name"/>
<display:column property="lastName"  title="Last Name"/>
<display:column property="address" title="Address"/>
<display:column property="city" title="City"/>
</display:table>

If we have observed clearly, we have used the property called ‘link’ and the decorator class ‘com.samples.LinkDecorator’. So, the decorator class appears as follows:

package com.samples;
import org.displaytag.decorator.TableDecorator;

/**
* @author vinod@pramati.com
* Sep 11, 2011
*/
public class LinkDecorator extends TableDecorator {
   public String getLink(){
      EmployeesBean empData = (EmployeesBean) getCurrentRowObject();
      String link = "<a href=\"nextpage.jsp?id=" + empData.getId() + "&amp;amp;amp;amp;amp;amp;amp;amp;amp;fname="+ empData.getFirstName()+"\">" + empData.getId() +"</a>";
      return link;
   }
}

The hyperlink(ID) will point to ‘http://<$IP>:<$PORT>/<$CONTEXT>/nextpage.jsp?id=<$ID>&fname=<$FirstName>’

Display using the group of details:

In this scenario, assume need to display the data based on city and first name. For this, the display tag appears as:

<display:table name="result">
<display:column property="id" title="ID" href="nextpage.jsp" paramId="id" paramProperty="id"/>
<display:column property="firstName" title="First Name" group="2"/>
<display:column property="middleName" title="Middle Name"/>
<display:column property="lastName"  title="Last Name"/>
<display:column property="address" title="Address"/>
<display:column property="city" title="City" group="1"/>
</display:table>

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)