Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:

Rename as a tutorial

...

Info
titleUsed versions

Webwork 2.2 beta 3 (but should apply to previous versions)
JasperReports 1.1.0
JDK 1.4.2

Right, let's begin.

Our Person class

We start by defining a simple POJO class: Person.java

...

Nothing special. Just your basic properties, constructor, getters and setters.

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.

...

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

Show me the Action

Code Block
titlecom.mevipro.test.action.JasperAction
package com.mevipro.test.action;

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

import net.sf.jasperreports.engine.JasperCompileManager;

import com.mevipro.test.Person;
import com.opensymphony.xwork.ActionSupport;

public class JasperAction extends ActionSupport {

	//basic List - it will serve as our dataSource later on
	private List myList;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork.ActionSupport#execute()
	 */
	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 everything in a list - normally, this should be coming from a
		 * database but for the sake of simplicity, I left that out
		 */
		myList = new ArrayList();
		myList.add(p1);
		myList.add(p2);
		myList.add(p3);
		myList.add(p4);

		/*
		 * Here we compile our xml jasper template to a jasper file.
		 * Note: this isn't exactly considered 'good practice'.
		 * You should either use precompiled jasper files (.jasper) or provide some kind of check
		 * to make sure you're not compiling the file on every request.
		 * If you don't have to compile the report, you just setup your data source (eg. a List)
		 */
		try {
			JasperCompileManager.compileReportToFile(
					"WW_WEBAPP/jasper/our_jasper_template.jrxml",
					"WW_WEBAPP/jasper/our_compiled_template.jasper");
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		//if all goes well ..
		return SUCCESS;
	}

	/**
	 * @return Returns the myList.
	 */
	public List getMyList() {
		return myList;
	}

}

...

Warning

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 a special XML page to define templates which will be compiled to .jasper files. These templates will be used to design the resulting report. It's pretty straightforward.
This is a handwritten version - for more complex versions I seriously suggest taking a look a the various GUI designers.

...

Tip

Use a logger (commons-logging, log4j, ..) to watch com.opensymphony.webwork.views.jasperreports in debug mode, if you have any troubles

Register the Action

Alright, time to add the action to our xwork.xml file:

...

This specifies the format to which the jasper file will be transformed. Possible values are: PDF, CSV, XLS and HTML.

Conclusion

You should now be able to execute http://localhost:8080/YOUR_WEBAPP/myJasperTest.action - and you should see a nice list of names.
WW provides probably the most elegant way to deal with JasperReport files; specify the location of the .jasper file, specify what dataSource you want to use, and there you go.