For integrating your jasper report into the web application with struts2-jasper-plugin,
you need to do the following things
struts.xml
struts.xml
<!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" /> <constant name="struts.action.excludePattern" value="/servlets/.*" /> <package name="default " namespace="/" extends="struts-default,json-default,jasperreport-default"> <result-types> <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult"/> </result-types> <action name="myJasperTest" class="com.jasper.admin.action.JasperAction"> <result name="success" type="jasper"> <param name="location">/WEB-INF/classes/jasper/our_compiled_template.jasper</param> <param name="dataSource">myList</param> <param name="format">HTML</param> <param name="imageServletUrl">/servlets/image?image=</param> </result> </action> </package> </struts>
in the above struts.xml observe orange color line, these lines are very important for struts-jasper-plugin
JasperAction.java
package com.jasper.admin.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.interceptor.ServletRequestAware; import net.sf.jasperreports.engine.JasperCompileManager; import com.opensymphony.xwork2.ActionSupport; /** * @author KONDA REDDY * */ public class JasperAction extends ActionSupport implements ServletRequestAware{ /** List to use as our JasperReports dataSource. */ private List<Person> myList; private HttpServletRequest request = null; /* (non-Javadoc) * @see org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(javax.servlet.http.HttpServletRequest) */ public void setServletRequest(HttpServletRequest request) { this.request = request; } public String execute() throws Exception { // Create some imaginary persons. Person p1 = new Person(new Long(1), "KONDA REDDY", "LINGAMDINNE"); Person p2 = new Person(new Long(2), "PRASAD REDDY", "LINGAMDINNE"); Person p3 = new Person(new Long(3), "VENKATA JAYA SAI DEEP KUMAR REDDY", "ANNAPUREDDY"); Person p4 = new Person(new Long(4), "KRANTHI KUMAR REDDY", "ANNAPUREDDY"); // Store people in our dataSource list (normally they would come from a database). myList = new ArrayList<Person>(); myList.add(p1); myList.add(p2); myList.add(p3); myList.add(p4); String dir = request.getRealPath("/"); // Normally we would provide a pre-compiled .jrxml file // or check to make sure we don't compile on every request. try { JasperCompileManager.compileReportToFile(dir+ "/WEB-INF/classes/jasper/our_jasper_template.jrxml", dir+ "/WEB-INF/classes/jasper/our_compiled_template.jasper"); } catch (Exception e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public List<Person> getMyList() { return myList; } }
Person.java
package com.jasper.admin.action; /** * @author KONDA REDDY * */ public class Person { private Long id; private String name; private String lastName; public Person() { } /** * @param name * @param lastName */ public Person(String name, String lastName) { this.name = name; this.lastName = lastName; } /** * @param id * @param name * @param lastName */ public Person(Long id, String name, String lastName) { this.id = id; this.name = name; this.lastName = lastName; } /** * @return the id */ public Long getId() { return id; } /** * @param id the id to set */ public void setId(Long id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the lastName */ public String getLastName() { return lastName; } /** * @param lastName the lastName to set */ public void setLastName(String lastName) { this.lastName = lastName; } }
our_jasper_template.jrxml
<?xml version="1.0" encoding="UTF-8"?> <!-- Created with Jaspersoft Studio version last--> <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="jasper_test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="30" bottomMargin="30" uuid="d22f49ae-9929-44f6-8c5c-ee34a0587e14"> <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/> <style name="Title" forecolor="#FFFFFF" fontName="Times New Roman" fontSize="50" isBold="false" pdfFontName="Times-Bold"/> <field name="name" class="java.lang.String"/> <field name="lastName" class="java.lang.String"/> <title> <band height="65" splitType="Stretch"> <frame> <reportElement uuid="19675ab3-ade7-4b13-b4f8-6af91e4d3bcf" mode="Opaque" x="0" y="0" width="555" height="65" forecolor="#006699" backcolor="#006699"/> <staticText> <reportElement uuid="19325fe6-91bb-4925-8637-30b76da773cb" style="Title" x="0" y="0" width="555" height="65"/> <textElement textAlignment="Right"> <font size="54" isBold="false"/> </textElement> <text><![CDATA[Konda Reddy]]></text> </staticText> </frame> </band> </title> <pageHeader> <band splitType="Stretch"/> </pageHeader> <columnHeader> <band height="20" splitType="Stretch"> <staticText> <reportElement uuid="6b8becc8-fd1a-441c-ad9f-e02c1acb220a" x="180" y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/> </textElement> <text><![CDATA[NAME]]></text> </staticText> <staticText> <reportElement uuid="5aabd0db-f280-48fa-a018-2e6055e8c701" x="360" y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/> </textElement> <text><![CDATA[LASTNAME]]></text> </staticText> </band> </columnHeader> <detail> <band height="20" splitType="Stretch"> <textField> <reportElement uuid="3265c8f6-f7e5-482d-b555-af4db49f206b" x="180" y="0" width="180" height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> </textField> <textField> <reportElement uuid="57a43440-4996-4ef4-87da-525f76f87da5" x="360" y="0" width="180" height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band splitType="Stretch"/> </columnFooter> <pageFooter> <band height="15" splitType="Stretch"> <staticText> <reportElement uuid="9c36d27e-b869-41d6-9424-f3900f7626c8" x="0" y="0" width="40" height="15"/> <textElement/> <text><![CDATA[Page:]]></text> </staticText> <textField> <reportElement uuid="d0ae07db-5f4d-458b-beb2-b49566c42e88" x="40" y="0" width="100" height="15"/> <textElement/> <textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression> </textField> </band> </pageFooter> <summary> <band splitType="Stretch"/> </summary> </jasperReport>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="starter" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" > <display-name>LKREDDY</display-name> <filter> <filter-name>action2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <!-- END SNIPPET: filter --> <filter-mapping> <filter-name>action2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>ImageServlet</servlet-name> <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ImageServlet</servlet-name> <url-pattern>/servlets/image</url-pattern> </servlet-mapping> <!-- Welcome file lists --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <session-config> <session-timeout>30</session-timeout> </session-config> </web-app>
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"> <%@ taglib uri="/struts-tags" prefix="s"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <script language="JavaScript"> function calcHeight() { //find the height of the internal page var the_height = document.getElementById('the_iframe').contentWindow.document.body.scrollHeight; //change the height of the iframe document.getElementById('the_iframe').height = the_height; } </script> <title>Insert title here</title> </head> <body> <s:url id="jasperList" action="myJasperTest"></s:url> <s:a href="%{jasperList}">jasperList</s:a> <iframe width="100%" id="the_iframe" onLoad="calcHeight();" src="myJasperTest" scrolling="NO" frameborder="0" height="1"> An iframe capable browser is required to view this report. </iframe> </body> </html>