Pages Navigation Menu

Coding is much easier than you think

How to get checkbox values from struts2 checkbox in displaytag to action class

Posted by in Display Tag, Struts 2 Tutorial, Struts-2 | 6 comments

 
Consider a scenario, such that we have a list of items and each item can be selected by checking its checkbox. If a submit button is clicked after selecting all necessary checkboxes. So now, in our Action class, we could get the values of checkboxes which had been selected, by which we can implement delete functionality.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 

The following snippet of code is used to retrieve the value from a checkbox used inside displaytag.

 

<display:table name="productList" pagesize="10" requestURI="productAction" id="row">

<display:column title="Delete">
<s:checkbox name="productList[%{#attr.row_rowNum - 1}].chkBox"
  id="check%{#attr.row_rowNum - 1}" value="%{#attr.row.chkBox}"/>
</display:column>

<display:column property="product" title="Product Name"/>
<display:column property="price" title="Price"/>

</display:table>
<s:submit action="removeProduct" value="Delete"
  onClick="return confirm('Do you want to delete these items?');"/>

 
In our Action class, we can get the values of checkbox by creating getter & setter for the field “chkBox”.
 

Read More

Struts 2 tag not working in Display tag – Solution

Posted by in Display Tag, Struts 2 Tutorial, Struts-2 | 1 comment

 
Consider a scenario, such that you want to display a value of a column based on certain condition.
 
For example:
 
if a student’s marks is greater than 50 display as ‘pass’, if its less than 50 then display as ‘fail’. For this scenario, in a display tag, most of us result in the following code snippet.
 


<display:table name="ranklist" id="row" pagesize="10"  requestURI="rankAction" >

<s:if test="%{mark > 50}">
<display:column title="Status">Pass</display:column>
</s:if>
<s:elseif test="%{mark < 50}">
<display:column title="Status">Fail</display:column>
</s:elseif>

</display:table>

 
But here unfortunately this code does not works, because the variable “mark”is not reachable inside the display tag, since it is not associated with current display table.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
Solution :
 
So the correct way of coding is:
 

<display:table name="ranklist" id="row" pagesize="10"  requestURI="rankAction" >

<display:column property="userName" title="User Name" />

<s:if test="%{#attr.row.mark > 50}">
<display:column title="Status">Pass</display:column>
</s:if>

<s:elseif test="%{#attr.row.mark < 50}">
<display:column title="Status">Fail</display:column>
</s:elseif>

</display:table>

 
Generalized way to access the struts 2 tag via display tag is :- #attr.tableIdName.tableField. So the only way to access a struts 2 tag inside displaytag, is to follow the above syntax.
 
Example : To access a property tag inside display tag :

<s:property name="userId" value="#attr.row.userId" />

 
Related Questions:

How to get checkbox values from displaytag using struts2
Displaytag export option is not working?
 

Read More

Pagination in Struts 2 Using display tag

Posted by in Display Tag, Struts-2 | 74 comments

 

 
In Struts 2 Pagination can be implemented with the help of Displaytag. This tag can handle feature such as pagination, sorting and exporting of a table. In the following example we will see how to display data using display tag and to do pagination, sorting and exporting.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
Download the Latest version of displaytag.jar here
 
Now Create a Dynamic web project and make sure that you have the following libraries are in your WEB-INF/lib directory.

commons-beanutils-1.8.0.jar
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-io-2.0.1.jar
commons-lang-2.4.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
displaytag-1.2.jar
freemarker-2.3.19.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar

displaytag-export-poi.jar is used to export the files in xls,csv or xml format.
 


 
Action Class
 
StudentAction.java
package com.simplecode.action;

import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.simplecode.form.StudentBean;

public class StudentAction extends ActionSupport
{
private static final long serialVersionUID = 1L;
private List<StudentBean> students = new ArrayList<StudentBean>();

public String displayStudentList()
{
	students.add(new StudentBean("o7bb002", "Gokul", "ECE", 4));
	students.add(new StudentBean("o7bc074", "Muthu Vijay", "CSE", 6));
	students.add(new StudentBean("o7bb040", "Jaya Prakash", "ECE", 10));
	students.add(new StudentBean("o7bc055", "Mohiadeen", "CSE", 7));
	students.add(new StudentBean("o7bd047", "HariPriya", "IT", 1));
	students.add(new StudentBean("o7bd024", "Pavithra", "IT", 3));
	students.add(new StudentBean("o7bb009", "Aswin", "ECE", 8));
	students.add(new StudentBean("o7ba029", "Sharmila", "IT", 11));
	students.add(new StudentBean("o7ba027", "Nilafar", "IT", 2));
	students.add(new StudentBean("o7bd081", "Dinesh Babu", "MECH", 13));
	students.add(new StudentBean("o7ba062", "Lourde", "MECH", 9));
	students.add(new StudentBean("o7bc079", "Nisha", "CSC", 5));
	students.add(new StudentBean("o7bb039", "Guru Prasad", "MECH", 12));
	students.add(new StudentBean("o7bc033", "Gowtham Raj", "CSE", 15));
	students.add(new StudentBean("o7bb039", "Ibrahim Sha", "ECE", 14));
	students.add(new StudentBean("o7bd081", "Dinesh Babu", "MECH", 16));

	return SUCCESS;
}

public List<StudentBean> getStudents()
{
	return students;
}

public void setStudents(List<StudentBean> students)
{
	this.students = students;
}
}

 
StudentBean.java
 

package com.simplecode.form;

public class StudentBean
{
private String rollNo;
private String studentName;
private String department;
private int rank;

public StudentBean(String rollNo, String studentName, String department, int rank)
{
    this.rollNo = rollNo;
    this.studentName = studentName;
    this.department = department;
    this.rank = rank;
}
public String getRollNo()
{
	return rollNo;
}
public void setRollNo(String rollNo)
{
	this.rollNo = rollNo;
}
public String getStudentName()
{
	return studentName;
}
public void setStudentName(String studentName)
{
	this.studentName = studentName;
}
public String getDepartment()
{
	return department;
}
public void setDepartment(String department)
{
	this.department = department;
}
public int getRank()
{
	return rank;
}
public void setRank(int rank)
{
	this.rank = rank;
}
}

 
displaytag.jsp
 


<%@taglib prefix="s" uri="/struts-tags"%>
<%@taglib prefix="display" uri="http://displaytag.sf.net"%>

<title>Students Details</title>
<style type="text/css">

/* for hiding the page banner */
.pagebanner
{
	display: none;
}
/* for customizing page links */
.pagelinks
{
	color: maroon;
	margin: 20px 0px 20px 50px;
}
/* for shifting all the Export options*/
.exportlinks
{
	margin: 20px 0px 20px 30px;
}
/* For changing the spaces between export link */
.export
{
	margin-left: 30px;
}
/* For Table css */
table
{
	border: 1px solid #666;
	width: 60%;
	margin: 20px 0 20px 0px;
}
/* For odd and even row decoration */
tr.odd
{
	background-color: #fff
}
tr.tableRowEven,tr.even
{
	background-color: #CCCCCC
}
/* Css for table elements */
th,td
{
	padding: 2px 4px 2px 4px;
	text-align: left;
	vertical-align: top;
}
thead tr
{
	background-color: #999999;
}
/* For changing the background colour while sorting */
th.sorted
{
	background-color: #CCCCCC;
}
th.sorted a,th.sortable a
{
	background-position: right;
	display: block;
	width: 100%;
}
th a:hover
{
	text-decoration: underline;
	color: black;
}
th a,th a:visited
{
	color: black;
}
</style>

<html>
<body>
<display:table id="studentTable" name="students" pagesize="5" cellpadding="5px;"
cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
<display:column property="rollNo" title="Roll No"/>
<display:column property="studentName" title="Student Name"/>
<display:column property="department" title="Department"/>
<display:column property="rank" title="Rank"/>
</display:table>
</body>
</html>

 
Questions:

How to make Struts 2 tag work inside Display tag?
How to get checkbox values from displaytag using struts2
Displaytag export option is not working?
 
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>
<package name="default" extends="struts-default">
<action name="displayStudentList" class="com.simplecode.action.StudentAction"
                  method="displayStudentList">
   <result name="success">/displaytag.jsp</result>
</action>
</package>
</struts>

 
After executing the project, you will get the following output:
 

 
Recommended reading :

  • AJAX implementation in Struts 2 using JQuery and JSON
  • Gridview in Struts2 using jQuery DataTable
  • CRUD Operations in Struts 2 using jTable jQuery plugin via Ajax
  •  
    Auto-sorting
     
    If you want to allow the user to sort the data displayed then set the attribute sortable=”true” on the display:column tag that you want to sort by.
     

    <display:column property="rollNo" title="Roll No" sortable="true"/>
    <display:column property="studentName" title="Student Name" sortable="true"/>
    <display:column property="department" title="Department" sortable="true"/>
    <display:column property="rank" title="Rank" sortable="true"/>
    

     
    Now on clicking the rank header the student resultset are sorted according to rank as shown below.


     
    Here the problem is whenever we sort the table by clicking on any sortable column header it only sort the data visible in that page. We can override this behavior by setting sort attribute in column to list as shown below.
     

    <display:table export="true" id="studentTable" name="students" pagesize="5" cellpadding="5px;"
    cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="" sort = "list">
    

     

    So now when you click on sort, the data will be sorted based on whole list.
     

     
    Data exporting
     
    When you set the Table Tag’s export attribute to “true”, footer will appear below the sorted result, which will allow you to export the data being shown in various formats(CSV, XLS, and XML).
     
    ** UPDATE: Struts 2 Complete tutorial now available here.
     

    <display:table export="true" id="studentTable" name="students" pagesize="5" cellpadding="5px;"
    cellspacing="5px;" style="margin-left:50px;margin-top:20px;" requestURI="">
    

     

     
    Suppose if you want to place the banner at bottom , then you can use the following code, within the display-table tag

    <display:setProperty name="paging.banner.placement" value="bottom" />
    

     
    ** Update — The below screenshot is the demo of pagination in struts 2 via ajax using using jQuery jTable plugin, this plugin has several inbuilt theme and lot may cool features like crud implementation via ajax, internalization etc, to know more about this plugin, please refer the article here.
     
    Struts-2-jTable-jQuery-plug-in-Create-record-animation-effect
     

    download
     

    Read More