Pages

Thursday, February 12, 2009

Jasper Report

  • Jasper Reports is an open-source Java reporting library.
  • 100% written in Java.
  • 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.
  • You can configure various data source: JDBC, EJB, POJO, Hibernate, XML.


Download Struts 2 Jasper Report Plugins

We need the following libraries: http://www.sourceforge.net/projects/jasperreports

  • jasperreports-version.jar
  • commons-*.jar
  • itext-version.jar
  • jdt-compiler.jar

Copy these libraries to your WEB-INF/lib directory

Code

Create Product JavaBean called Product.java

Create Action (MyJasperAction) which compiles and stream the datasource to jrxml which compiles and generate report.



public
class MyJasperAction extends ActionSupport {
 
    /** create List to use as our JasperReports dataSource. */
    private List myList;
 
    public String execute() throws Exception {
 
        Product p1 = new Product(new Long(1), "Sony Camera", "149.99");
        Product p2 = new Product(new Long(2), "Fuji Camera", "99.99");
 
        // Store product in our dataSource list (normally would come from database).
        myList = new ArrayList();
        myList.add(p1);
        myList.add(p2);
 
        // Normally we would provide a pre-compiled .jrxml file
        // or check to make sure we don't compile on every request.

try {

JasperCompileManager.compileReportToFile(

"myapp/reports/jasper_template.jrxml",

"myapp/reports/compiled_template.jasper");

} catch (Exception e) {

e.printStackTrace();

return ERROR;

}


        return SUCCESS;
    }
 
    public List getMyList() {
        return myList;
    }
}

Struts.xml

define the jasper result type manually in struts.xml


Google
 

Java-Struts