DisplayTag with Struts2 | Code Factory
2 min readNov 22, 2019
Donate : Link
Reference Link : Link
Applications : Link
Part 1
Part 2
Download code and jars : Link
File : emp.java
package com.codeFactory;public class emp {
private String name;
private String email;public emp(String name, String email) {
this.name = name;
this.email = email;
}public String getName() {
return name;
}public String getEmail() {
return email;
}}
File : getData.java
package com.codeFactory;
package com.codeFactory;import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.interceptor.ServletRequestAware;import com.opensymphony.xwork2.ActionSupport;public class getData extends ActionSupport implements ServletRequestAware{HttpServletRequest request;
public String execute()
{
List<emp> empList = new ArrayList<emp>();
empList.add(new emp("admin", "admin@p.co"));
empList.add(new emp("patel", "patel@p.co"));
empList.add(new emp("emp1", "emp1@p.co"));
empList.add(new emp("emp2", "emp2@p.co"));
empList.add(new emp("emp3", "emp3@p.co"));
request.setAttribute("empList", empList);
System.out.println("request set");
return SUCCESS;
}@Override
public void setServletRequest(HttpServletRequest request) {
// TODO Auto-generated method stub
this.request = request;
}
public HttpServletRequest getServletRequest()
{
return request;
}
}
File : struts.xml
<?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" />
<include file="struts-default.xml" />
<package name="a" extends="struts-default">
<action name="disTag" class="com.codeFactory.getData">
<result name="success">displayTag.jsp</result>
</action>
</package>
</struts>
File : web.xml
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5"><display-name>DisplayTag</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list><filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter><filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
File : index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<title>Index page</title>
</head>
<body>
<a href="disTag">Display Tag</a>
</body>
</html>
File : displayTag.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://displaytag.sf.net" prefix="display"%>
<!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">
<title>Display tag example.</title>
<style type="text/css">
table {
border: 1px solid #666;
width: 80%;
margin: 20px 0 20px 0 !important;
}th, td {
padding: 2px 4px 2px 4px !important;
text-align: left;
vertical-align: top;
}thead tr {
background-color: #999999;
}th.sorted {
background-color: #CCCCCC;
}th a, th a:visited {
color: black;
}th a:hover {
text-decoration: underline;
color: black;
}th.sorted a, th.sortable a {
background-position: right;
display: block;
width: 100%;
}tr.odd {
background-color: #fff
}tr.tableRowEven, tr.even {
background-color: #CCCCCC
}
</style></head>
<body>
<center>
<display:table name="empList" id="empList" pagesize="3" requestURI="">
<display:column property="name" sortable="true"></display:column>
<display:column property="email" sortable="true" autolink="true"></display:column>
</display:table>
</center>
</body>
</html>