How to fixed table header in struts2 using display table
In Struts 2, fixed table header can be implemented with the help of display tag library. In this article, We will see fixed table header, pagination, sorting and exporting data in different formats like excel, CSV etc. In this example, We are using eclipse IDE, Struts 2.5.10.1 and DisplayTag 1.2.
Download the displaytag library from Sourceforge Official Site. Now follow below steps to create a dynamic web project using eclipse.
1. Open Eclipse and Go to “File” and choose “Dynamic Web Project” in “New” option.
2. “New Dynamic Web Project” window will appear. Enter project name “Struts2DisplayTag” and click on “‘Next” button.
3. Click on “Next” button.
4. Choose “Generate web.xml deployment descriptor” and Click on “Finish” button.
5. The project created successfully. Copy the following jar file from “WEB-INF/lib” directory.
[code lang=”css”]
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-fileupload-1.3.2.jar
commons-io-2.4.jar
commons-lang-2.3.jar
commons-lang3-3.4.jar
displaytag-1.2.jar
displaytag-export-poi-1.2.jar
freemarker-2.3.23.jar
itext-1.3.jar
javassist-3.20.0-GA.jar
jcl104-over-slf4j-1.4.2.jar
jstl-1.1.2.jar
log4j-1.2.13.jar
log4j-api-2.7.jar
ognl-3.1.12.jar
poi-3.2-FINAL.jar
slf4j-api-1.4.2.jar
slf4j-log4j12-1.4.2.jar
standard-1.0.6.jar
struts2-core-2.5.10.1.jar
[/code]
6. Creating Bean Class.
UserBean.java
[code lang=”java”]
public class UserBean {
private int ID;
private String FIRST_NAME;
private String LAST_NAME;
private String EMAIL_ID;
private String PHONE_NO;
//Constructor
public UserBean() {
super();
}
public UserBean(int iD, String fIRST_NAME, String lAST_NAME, String eMAIL_ID, String pHONE_NO) {
super();
ID = iD;
FIRST_NAME = fIRST_NAME;
LAST_NAME = lAST_NAME;
EMAIL_ID = eMAIL_ID;
PHONE_NO = pHONE_NO;
}
//Getter and Setter
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getFIRST_NAME() {
return FIRST_NAME;
}
public void setFIRST_NAME(String fIRST_NAME) {
FIRST_NAME = fIRST_NAME;
}
public String getLAST_NAME() {
return LAST_NAME;
}
public void setLAST_NAME(String lAST_NAME) {
LAST_NAME = lAST_NAME;
}
public String getEMAIL_ID() {
return EMAIL_ID;
}
public void setEMAIL_ID(String eMAIL_ID) {
EMAIL_ID = eMAIL_ID;
}
public String getPHONE_NO() {
return PHONE_NO;
}
public void setPHONE_NO(String pHONE_NO) {
PHONE_NO = pHONE_NO;
}
}
[/code]
7. Creating Action Class.
DisplayTableAction.java
[code lang=”java”]
import java.util.ArrayList;
import java.util.List;
import com.opensymphony.xwork2.ActionSupport;
import com.tech2our.bean.UserBean;
public class DisplayTableAction extends ActionSupport{
private List<UserBean> userBeans;
@Override
public String execute() throws Exception {
loadData();
return SUCCESS;
}
public void loadData(){
userBeans = new ArrayList<UserBean>();
userBeans.add(new UserBean(1,"Jack", "Nicholson","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(2,"Marlon", "Brando","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(3,"Robert", "De Niro","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(4,"Al", "Pacino","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(5,"Daniel", "Day-Lewis","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(6,"Dustin", "Hoffman","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(7,"Tom", "Hanks","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(8,"Anthony", "Hopkins","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(9,"Paul", "Newman","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(10,"Denzel", "Washington","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(11,"Spencer", "Tracy","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(12,"Laurence", "Olivier","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(13,"Jack", "Lemmon","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(14,"Michael", "Caine","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(15,"Robin", "Williams","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(16,"Robert", "Duvall","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(17,"Sean", "Penn","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(18,"Morgan", "Freeman","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(19,"Jeff", " Bridges","test@tech2our.com","00-00000000"));
userBeans.add(new UserBean(20,"Sidney", "Poitier","test@tech2our.com","00-00000000"));
}
public List<UserBean> getUserBeans() {
return userBeans;
}
public void setUserBeans(List<UserBean> userBeans) {
this.userBeans = userBeans;
}
}
[/code]
8. Creating JSP file.
DisplayTableExample.jsp
[code lang=”java”]
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="display" uri="http://displaytag.sf.net/el"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
<link rel="stylesheet" href="css/style.css"/>
<title>Display Table Example</title>
</head>
<body>
<h1>Display Table Example</h1>
<display:table id="userTable" uid = "userTable" name="userBeans" requestURI="" export="true" pagesize="10" class="table table-striped table-bordered">
<display:column property="ID" title="Id" sortable="true"/>
<display:column property="FIRST_NAME" title="First Name" sortable="true"/>
<display:column property="LAST_NAME" title="Last Name" sortable="true"/>
<display:column property="EMAIL_ID" title="Email" sortable="true"/>
<display:column property="PHONE_NO" title="Phone" sortable="true"/>
<display:setProperty name="paging.banner.placement" value="bottom" />
<display:setProperty name="export.pdf.filename" value="ActorDetails.pdf"/>
</display:table>
</body>
</html>
[/code]
9. Configure “Struts.xml” and “web.xml” file.
struts.xml
[code lang=”java”]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.devMode" value="true" />
<package name="basicstruts2" extends="struts-default">
<action name="displayTableAction" class="com.tech2our.action.DisplayTableAction">
<result>/DisplayTableExample.jsp</result>
</action>
</package>
</struts>
[/code]
web.xml
[code lang=”java”]
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Struts2DisplayTag</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
[/code]
Congratulations!!! You have created project.
10. Right click on project and Click on “Run on Server” option in “Run As”. Program will display in browser like below.