JasperReports Tutorial

The Struts 2 JasperReports plugin is a bridge from Struts 2 to JasperReports and does not include JasperReports itself, which must be downloaded separately.

JasperReports is one of the leading open-source Java reporting libraries. It compiles .jrxml (XML source) to .jasper (compiled) files, which in turn can be transformed into several output types including PDF, HTML, CSV, and XLS.

In the following example, we will use the framework to create a PDF with a list of persons. Our action will be used to create a List with Person objects, and our JasperReports Result will use this list to fill our template, and return the PDF.

Our Person class

We start by defining a simple Person POJO class.

com.mevipro.test.Person.java
package com.mevipro.test; public class Person { private Long id; private String name;private String lastName; public Person() { } public Person(String name, String lastName) {this.name = name; this.lastName = lastName; } public Person(Long id, String name, StringlastName) { this.id = id; this.name = name; this.lastName = lastName; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getLastName() {return lastName; } public void setLastName(String lastName) { this.lastName = lastName; }public String getName() { return name; } public void setName(String name) { this.name = name; } }

JasperReports libraries

Before we can continue, we need to add the JR libraries to our classpath. You can download the JR project here: http://www.sourceforge.net/projects/jasperreports
Save the jasperreports-X-project.zip to your harddisk, and extract the files.

We need the following files:

  • dist/jasperreports-X.jar
  • lib/commons-*.jar (all the commons - except maybe for commons-logging)
  • lib/itext-X.jar
  • lib/jdt-compiler.jar

Copy these jars over to your S2_WEBAPP/WEB-INF/lib directory, and add them to your classpath.

Creating the Action

com.mevipro.test.action.JasperAction

package com.mevipro.test.action; import java.util.ArrayList; import java.util.List; importnet.sf.jasperreports.engine.JasperCompileManager; import com.mevipro.test.Person; importcom.opensymphony.xwork.ActionSupport; public class JasperAction extends ActionSupport {private List<Person> myList; public String execute() throws Exception { // Create some imaginary persons. Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie"); Person p2 = new Person(new Long(2), "Jason", "Carrora"); Person p3 = new Person(new Long(3),"Alexandru", "Papesco"); Person p4 = new Person(new Long(4), "Jay", "Boss"); // Store people in our dataSource list (normally would come from database). myList = newArrayList<Person>(); myList.add(p1); myList.add(p2); myList.add(p3); myList.add(p4); // Normally we would provide a pre-compiled .jrxml file // or check to make sure we don't compile on every request. try { JasperCompileManager.compileReportToFile("S2_WEBAPP/jasper/our_jasper_template.jrxml","S2_WEBAPP/jasper/our_compiled_template.jasper"); } catch (Exception e) { e.printStackTrace(); return ERROR; } return SUCCESS; } public List<Person> getMyList() {return myList; } }

Again, don't use this in production code. You should of course either provide compiled templates, or do some sort of checking to avoid compiling the template on every request. But for our demonstration, or development, this suits our needs just fine.

Our Jasper template

JR uses XML configuration to define templates which are compiled to .jasper files. These templates define the resulting report.
This is a handwritten version - for more complex versions I seriously suggest taking a look a the various GUI designers.

our_jasper_template.jrxml
<?xml version="1.0"?> <!DOCTYPE jasperReport PUBLIC "-//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"><jasperReport name="jasper_test"> <!-- Our fields from the Person class. --> <field name="name" class="java.lang.String"/> <field name="lastName"class="java.lang.String"/> <title> <band height="50"> <staticText> <reportElement x="0" y="0" width="180" height="15"/> <textElement/> <text><![CDATA[Struts 2 JasperReports Sample]]></text> </staticText> </band> </title> <pageHeader> <band/></pageHeader> <columnHeader> <band height="20"> <staticText> <reportElement x="180"y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/></textElement> <text><![CDATA[NAME]]></text> </staticText> <staticText> <reportElement x="360" y="0" width="180" height="20"/> <textElement> <font isUnderline="true"/></textElement> <text><![CDATA[LASTNAME]]></text> </staticText> </band> </columnHeader><detail> <band height="20"> <textField> <reportElement x="180" y="0" width="180"height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression> </textField> <textField> <reportElement x="360" y="0"width="180" height="15"/> <textElement/> <textFieldExpression><![CDATA[$F{lastName}]]></textFieldExpression> </textField> </band> </detail> <columnFooter> <band/></columnFooter> <pageFooter> <band height="15"> <staticText> <reportElement x="0"y="0" width="40" height="15"/> <textElement/> <text><![CDATA[Page:]]></text></staticText> <textField> <reportElement x="40" y="0" width="100" height="15"/><textElement/> <textFieldExpression class="java.lang.Integer"><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression> </textField> </band> </pageFooter><summary> <band/> </summary> </jasperReport>

Save this file in S2_WEBAPP/jasper/ as 'our_jasper_template.jrxml'.

Registering the Action

Using the JasperReports plugin requires adding the JasperReports result type as well as normal action configuration.

struts.xml
<package name="default" namespace="/" extends="jasperreports-default"> <action name="myJasperTest" class="com.mevipro.test.action.JasperAction"> <result name="success" type="jasper"> <param name="location">/jasper/our_compiled_template.jasper</param> <param name="dataSource">myList</param> <param name="format">PDF</param> </result> </action> ... </package>
posted @ 2017-06-29 09:52  我不是个码农  阅读(162)  评论(0)    收藏  举报