Struts display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model.
The library provides a significant amount of functionality while still being easy to use.
Displaytag can handle column display, sorting, paging, cropping, grouping, exporting, smart linking and decoration of a table in a customizable XHTML style.
In the following example we will see how to dispaly data using display tag and to do pagination and sorting.
We will use Eclipse as an IDE for our example.
Step 1: Create Eclipse dynamic web project and copy JAR files
Start Eclipse and goto File -> New -> Project -> Dynamic Web Project
Following is the list of required JAR files to be added in Java Class Path of your project. Download displaytag JAR files from
http://displaytag.sourceforge.net/1.2/download.html.
Step 2: Create Action, Form and Bean class
Once the project is created, create 3 java files ForbesData, UserAction and UserForm in package net.viralpatel.struts.displaytag.
ForbesData is a bean class which consists of setter ,getter methods and properties.
The class code is shown below.
package net.viralpatel.struts.displaytag;
import java.util.ArrayList;
public class ForbesData {
private int rank;
private String name;
private int age;
private double netWorth;
public ForbesData() {
//no-argument constructor
}
public ForbesData(int rank, String name, int age, double netWorth) {
this.rank = rank;
this.name = name;
this.age = age; //parameterized constructor
this.netWorth = netWorth;
}
public ArrayList loadData() {
ArrayList userList = new ArrayList();
userList.add(new ForbesData(1, "William Gates III", 53, 40.0));
userList.add(new ForbesData(2, "Warren Buffett", 78, 37));
userList.add(new ForbesData(3, "Carlos Slim Helu & family", 69, 35));
userList.add(new ForbesData(4, "Lawrence Ellison", 64, 22.5));
userList.add(new ForbesData(5, "Ingvar Kamprad & family", 83, 22));
userList.add(new ForbesData(6, "Karl Albrecht", 89, 21.5));
userList.add(new ForbesData(7, "Mukesh Ambani", 51, 19.5));
userList.add(new ForbesData(8, "Lakshmi Mittal", 58, 19.3));
userList.add(new ForbesData(9, "Theo Albrecht", 87, 18.8));
userList.add(new ForbesData(10, "Amancio Ortega", 73, 18.3));
userList.add(new ForbesData(11, "Jim Walton", 61, 17.8));
userList.add(new ForbesData(12, "Alice Walton", 59, 17.6));
userList.add(new ForbesData(12, "Christy Walton & family", 54, 17.6));
userList.add(new ForbesData(12, "S Robson Walton", 65, 17.6));
userList.add(new ForbesData(15, "Bernard Arnault", 60, 16.5));
userList.add(new ForbesData(16, "Li Ka-shing", 80, 16.2));
userList.add(new ForbesData(17, "Michael Bloomberg", 67, 16));
userList.add(new ForbesData(18, "Stefan Persson", 61, 14.5));
userList.add(new ForbesData(19, "Charles Koch", 73, 14));
userList.add(new ForbesData(19, "David Koch", 68, 14));
userList.add(new ForbesData(21, "Liliane Bettencourt", 86, 13.4));
userList.add(new ForbesData(22, "Prince Alwaleed Bin Talal Alsaud", 54, 13.3));
return userList;
}
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getNetWorth() {
return netWorth;
}
public void setNetWorth(double netWorth) {
this.netWorth = netWorth;
}
}
Create a UserForm class which extends ActionForm
Code is shown below
package net.viralpatel.struts.displaytag;
import java.util.ArrayList;
public class UserForm extends org.apache.struts.action.ActionForm {
private ArrayList forbesList;
public ArrayList getForbesList() {
return forbesList;
}
public void setForbesList(ArrayList forbesList) {
this.forbesList = forbesList;
}
}
Create an action class name Useraction
package net.viralpatel.struts.displaytag;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class UserAction extends Action {
private final static String SUCCESS = "success";
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm) form;
ForbesData actorData = new ForbesData();
userForm.setForbesList(actorData.loadData());
return mapping.findForward(SUCCESS);
}
}
Step 3: Create JSPs, struts-config.xml and web.xml
Create index.jsp and user.jsp in WebContent folder and struts-config.xml and web.xml in WebContent/WEB-INF folder.
Create
index.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
//to farward action to userAction.do
Create
user.jsp
<%@taglib uri="http://displaytag.sf.net" prefix="display" %>
Add html tag
The World's Billionaires 2009 - Forbes List
close html tag
struts-config.xml
create web.xml
action
org.apache.struts.action.ActionServlet
config/WEB-INF/struts-config.xml
debug2
detail2
2
action
*.do
30
index.jsp
Excute the project,the output looks like below