Struts 2 and Jasper Report Integration Example


This struts 2 tutorials explain how to integrate Jasper iReport in your dynamic web application to generate the PDF report. Before starting the coding, we need to include Jasper iReport JAR dependency in Struts 2 project.

Software Used

  1. Eclipse
  2. MySQL Database
  3. JDK 8
  4. Tomcat 8
  5. iReport-5.6.0

Required JARS

Struts 2 and Jasper iReport integration required these jar files.

  1. asm-3.3.jar
  2. asm-commons-3.3.jar
  3. asm-tree-3.3.jar
  4. commons-beanutils-1.8.0.jar
  5. commons-collections-3.1.jar
  6. commons-digester-2.0.jar
  7. commons-fileupload-1.2.2.jar
  8. commons-logging-api-1.1.jar
  9. commons-io-2.0.1.jar
  10. commons-lang-2.5.jar
  11. commons-logging-1.1.3.jar
  12. freemarker-2.3.18.jar
  13. jasperreports-5.6.0.jar
  14. jasperreports-javaflow-5.6.0.jar
  15. javassist-3.11.0.GA.jar
  16. ognl-3.0.4.jar
  17. struts2-core-2.3.1.2.jar
  18. xwork-core-2.3.1.2.jar
  19. mysql-connector-java-5.1.38-bin

Project Structure in Eclipse

Struts 2 and Jasper Report Integration Example

How to Start Coding

Sometimes it’s a big deal which code wrote first or how to start coding? To get your code error free to follow these steps…

Step- 1: Very first step is to create the table script.

CREATE TABLE `pdfreport` (
  `NAME` varchar(20) DEFAULT NULL,
  `EMAIL` varchar(50) DEFAULT NULL,
  `MOBILE` varchar(12) DEFAULT NULL,
  `COLLEGE` varchar(50) DEFAULT NULL,
  `COURSE` varchar(10) DEFAULT NULL,
  `SKILLS` varchar(100) DEFAULT NULL,
  `MARKS1` int(3) DEFAULT NULL,
  `MARKS2` int(3) DEFAULT NULL,
  `MARKS3` int(3) DEFAULT NULL
);

Step– 2: Design your report.

studentReport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
	name="studentReport" pageWidth="595" pageHeight="842" columnWidth="555"
	leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"
	uuid="ba63635c-ad40-41b0-9974-2f6d4d62ac4e">
	<property name="ireport.zoom" value="1.0" />
	<property name="ireport.x" value="0" />
	<property name="ireport.y" value="0" />
	<parameter name="logo" class="java.lang.String" />
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="NAME" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="email" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="mobile" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="college" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="course" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="skills" class="java.lang.String">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="marks1" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="marks2" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<field name="marks3" class="java.lang.Integer">
		<fieldDescription><![CDATA[]]>
		</fieldDescription>
	</field>
	<background>
		<band splitType="Stretch" />
	</background>
	<title>
		<band height="79" splitType="Stretch">
			<image>
				<reportElement x="0" y="0" width="208" height="59"
					uuid="5dfbe355-b454-49e2-a674-d44dac7559c1" />
				<imageExpression><![CDATA[$P{logo}]]></imageExpression>
			</image>
			<line>
				<reportElement x="0" y="69" width="552" height="2"
					forecolor="#FFFF00" uuid="fb69bdb0-2581-4077-9048-32a081feac19" />
			</line>
		</band>
	</title>
	<pageHeader>
		<band height="35" splitType="Stretch">
			<staticText>
				<reportElement mode="Opaque" x="0" y="0" width="552"
					height="35" backcolor="#CCCCCC" uuid="0baae80b-49e8-4cc1-84e5-84c4dc4deccc" />
				<textElement textAlignment="Center">
					<font size="26" />
				</textElement>
				<text><![CDATA[Student Reports]]></text>
			</staticText>
		</band>
	</pageHeader>
	<columnHeader>
		<band height="41" splitType="Stretch" />
	</columnHeader>
	<detail>
		<band height="123" splitType="Stretch">
			<textField>
				<reportElement x="0" y="71" width="185" height="20"
					uuid="45be6758-b007-4fc8-bed2-d805dd1212a1" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{marks1}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="185" y="71" width="185" height="20"
					uuid="e2900688-a1e6-4bd6-824f-1fec11357fa2" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{marks2}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="370" y="71" width="185" height="20"
					uuid="e948ed81-1b35-4801-972a-cb3721978c6b" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{marks3}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement mode="Opaque" x="0" y="51" width="185"
					height="20" backcolor="#CCCCCC" uuid="1d29d647-fff0-4523-b12e-a9b9ee1e7f85" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Subject 1]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="185" y="51" width="185"
					height="20" backcolor="#CCCCCC" uuid="be9225ad-4eea-49a9-aa82-20ee92ad365c" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Subject 2]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="370" y="51" width="185"
					height="20" backcolor="#CCCCCC" uuid="ccd258aa-657e-44d3-8462-79c384616791" />
				<box>
					<pen lineWidth="0.25" />
					<topPen lineWidth="0.25" />
					<leftPen lineWidth="0.25" />
					<bottomPen lineWidth="0.25" />
					<rightPen lineWidth="0.25" />
				</box>
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Subject 3]]></text>
			</staticText>
			<textField>
				<reportElement x="0" y="20" width="92" height="20"
					uuid="fc8f42e0-0ae0-493e-9a4f-3e695d71be7e" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="92" y="20" width="92" height="20"
					uuid="a56767ed-655c-4cb4-b9b2-3a0bfb1a0a26" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="184" y="20" width="92" height="20"
					uuid="193b7881-25dc-445f-af6a-f26232466fa3" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{mobile}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="276" y="20" width="92" height="20"
					uuid="abc9a75c-5118-4172-be15-7ad75414095d" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{college}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="368" y="20" width="92" height="20"
					uuid="2b350d01-3274-43ae-8bd4-c3feaf277b69" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{course}]]></textFieldExpression>
			</textField>
			<textField>
				<reportElement x="460" y="20" width="92" height="20"
					uuid="1239ea7f-5ca4-4aa4-80d3-9d1f4c3dad06" />
				<textElement textAlignment="Center" />
				<textFieldExpression><![CDATA[$F{skills}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement mode="Opaque" x="0" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="a0f72a52-fdac-49cd-9845-ea82ca2b7d93" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[NAME]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="92" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="0d30c4d9-52ed-4965-815e-4fa98dc98b87" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Email]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="184" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="68f75f19-987f-4f38-a307-38f35a819608" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Mobile]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="276" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="ed266987-3428-446d-881d-ba7057107025" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[College]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="368" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="12906bc7-1143-4064-bf6c-fc2c38c9b58c" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Course]]></text>
			</staticText>
			<staticText>
				<reportElement mode="Opaque" x="460" y="0" width="92"
					height="20" backcolor="#CCCCCC" uuid="ebba8c0c-fcfc-424d-844c-8d5ca576b05d" />
				<textElement textAlignment="Center">
					<font isBold="true" />
				</textElement>
				<text><![CDATA[Skills]]></text>
			</staticText>
			<line>
				<reportElement x="0" y="107" width="552" height="1"
					uuid="2221b6a3-4dbc-4719-ac8b-14e9313f4463" />
			</line>
		</band>
	</detail>
	<columnFooter>
		<band splitType="Stretch" />
	</columnFooter>
	<pageFooter>
		<band height="1" splitType="Stretch" />
	</pageFooter>
	<summary>
		<band splitType="Stretch" />
	</summary>
</jasperReport>      

Step– 3: Include all required jars in the project lib folder.

Step– 4: Add Struts 2 filter in web.xml

web.xml

<web-app>
        <display-name>Struts2JasperReportIntegration</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.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

Step– 5: Create a DAO class.

ReportDAO.java

package org.websparrow;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class ReportDAO {

	public ResultSet getPdf() {

		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
			String query = "SELECT NAME, email, mobile, college, course, skills, marks1, marks2, marks3 FROM pdfreport";
			Statement stmt = con.createStatement();
			ResultSet rs = stmt.executeQuery(query);

			return rs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}  

Step– 6: Create Action class.

ReportAction.java

package org.websparrow;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.ResultSet;
import java.util.HashMap;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;

public class ReportAction extends ActionSupport {

	public ResultSet rs = null;
	public String submit = null;
	public InputStream fileInputStream;
	public String jasperPath = "";
	public String pdfName = "";
	public String rpt = "";

        //Generate Getters and Setters... 

	@Override
	public String execute() throws Exception {
		try {
			if (submit.equals("pdf")) {
				rs = new ReportDAO().getPdf();
				jasperPath = ServletActionContext.getServletContext().getRealPath("/Reports");
				pdfName = "Student Report";
				rpt = "studentReport.jrxml";
				JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
				HashMap<String, Object> pm = new HashMap<String, Object>();
				String logo = jasperPath + "/ws.jpg";
				pm.put("logo", logo);
				JasperReport jr = JasperCompileManager.compileReport(jasperPath + "/" + rpt);
				JasperPrint jp = JasperFillManager.fillReport(jr, pm, resultSetDataSource);
				JasperExportManager.exportReportToPdfFile(jp, jasperPath + pdfName + ".pdf");
				fileInputStream = new FileInputStream(new File(jasperPath + pdfName + ".pdf"));

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SUCCESS";

	}	
} 

Step– 7: Design your User Interface page.

index.jsp

<%@taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<style type="text/css">
button {
	background-color: #008CBA;
	border: none;
	color: white;
	padding: 15px 32px;
	text-align: center;
	text-decoration: none;	
	display: inline-block;
	font-size: 16px;
	border-radius: 12px;
}
</style>
</head>
<body style="text-align: center;">
	<h1>Struts2 and Jasper iReport Integration Example</h1>
	<form method="POST" name="rpt" action="StudentReport.action">

		<button type="submit" name="submit" value="pdf">Generate Report</button>

	</form>
</body>
</html> 

Step– 8: Map the Action class in 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" />
	<package name="default" extends="struts-default" namespace="/">
		<action name="StudentReport" class="org.websparrow.ReportAction">
			<result name="SUCCESS" type="stream">
				<param name="contentType">application/octet-stream</param>
				<param name="inputName">fileInputStream</param>
				<param name="contentDisposition">attachment;filename="${pdfName}.pdf"</param>
				<param name="bufferSize">1024</param>
			</result>
		</action>
	</package>
</struts>

Step- 9: Finally all is done, now start your server and run the project.

Output :

Find the URL to test the application localhost:8080/Struts2JasperReportIntegration/. We will get the output as given below.

Screen 1

Struts 2 and Jasper Report Integration Example

Screen 2
Struts 2 and Jasper Report Integration Example

Screen 3
Struts 2 and Jasper Report Integration Example


Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.