Report with ZK Jasperreport Component

Grace Lin, Engineer, Potix Corporation
January 18, 2008

Version

Applicable to ZK 3.0.2 Freshly (zk-bin-3.0.2-FL-2008-01-17 and later)

Introduction

JasperReports is an open source Java reporting tool that can write to screen, to a printer or into PDF files. It can be used in Java-enabled applications, including J2EE or Web applications, to generate dynamic content. Before this article, we have ever show report in ZK by writing ZK glue codes (Report with ZK), but it is more difficult than directly using a component. Now we can use the report component to do this easily.

Usage of JasperReport Component

The following is the example code to use the report component.
The CustomDataSource class is a data source which implements JRDataSource.


<?xml version="1.0" encoding="utf-8"?>

<window title="jasperreport demo" border="normal">
    <borderlayout height="100%">
        <north maxsize="24" size="24" border="0">
            <button label="Report!" onClick="showReport()" />
        </north>
        <center border="none" flex="true">
            <jasperreport id="report" />
        </center>
    </borderlayout>
    <zscript><![CDATA[
        import java.util.*;
        
        void showReport() {
            //Preparing parameters
            Map parameters = new HashMap();
            parameters.put("ReportTitle", "Address Report");
            parameters.put("DataFile", "CustomDataSource.java");
            
            report.setSrc("DataSourceReport.jasper");
            report.setParameters(parameters);
            report.setDatasource(new CustomDataSource());
        }
    ]]></zscript>
</window>

The end user request a report by clicking the button and the "onClick" event handler will call showReport(), the report component will be set source path to get the PDF file and show on the page.



If you want to show report directly, you can set template source on the report component's attributes. Then, also set the data source and parameters of the report, like the following code.


<?xml version="1.0" encoding="utf-8"?>

<window title="jasperreport demo" border="normal">
    <zscript>
        Map parameters = new HashMap();
        parameters.put("ReportTitle", "Address Report");
        parameters.put("DataFile", "CustomDataSource.java");
        
        CustomDataSource ds = new CustomDataSource();
    </zscript>
        
    <jasperreport src="DataSourceReport.jasper"
        parameters="${parameters}" datasource="${ds}" />
</window>


Download

  1. jasperreport_demo.war

Conclusion

ZK developers can show report directly by using report component now. Set the template source, data source and parameters as same as what you do with JasperReports, then the component can show it easily. If you have any question, please feel free to leave comment here or post to ZK forum.

Comments
 
markus
2008-01-18

Thanx for the write-up Grace! However, I have a problem when I try to implement jasper reporting in my application. I get a "Component definition not found: jasperreport" failure. I installed zk-bin-3.0.2-FL-2008-01-17. Can you help me out? How do I make the jasperreport component known?

Tom
2008-01-18

You have to copy jasperreports.jar to your Web application.

Fernando
2008-01-18

This is very usefull. Thank's.

I have a question: How can i pass multiple datasources to the report? For example, a report may have a graph and a table each one with a diferente datasource.

Thank's again

Fabio
2008-01-18

Hi,

I have some questions!.. what about the advance configuration? I mean virtualization? (really big reports), subreports (jasper reports paths), I18N (classpath properties). outputs (pdf, xml, etc..), integration with others addons (dynamic jasper)?, can I send it to email or jackrabbit? (content repository)
it need acrobat reader? or Ms Office?

well sorry for too many questions, but you know these are basic capabilities that are required!.

hw_alex
2008-01-21

报表组件支持中文吗

Grace
2008-01-21

Dear Fernando,

I remember one JasperReport can only use a kind of data source at once. If you want a report which has graph and table, try to design both in the jrxml file, or try to use subreport. You can reference the sample of JasperReports, called JFreeChart Sample and Subreport Sample. Also can reference following sample.
http://ireport.sourceforge.net/swf/Subreport_viewlet_swf.htm

Valentin
2008-02-06

Why this approach http://www.zkoss.org/smalltalks/zkreport/zkreport.dsp doesn't work with ZK 3.0.2? Is your <jasperreport /> is the only way to work with jasper? Could you show me more ways to show jasper reports through ZK applications?

its me
2008-02-08

how to convert report to excel, please give me example code...

ehols
2008-02-20

To excel...try like this:report.setType("xls");

Arun
2008-02-25

I have a senario, in which I need to display a table rows dynamichally. Suppose one product is listed in 3 states, second in 5 states and third in no state. Based on the product selected, I need to show the details in a table such if I choose first product, the details should display in a table with 3 rows, table with 5 rows for second and no table should display for the third product. How do I do it?
Can a report be created without a .jasper or .jarxml file?

Arun
2008-02-25

I have a senario, in which I need to display a table rows dynamichally. Suppose one product is listed in 3 states, second in 5 states and third in no state. Based on the product selected, I need to show the details in a table such if I choose first product, the details should display in a table with 3 rows, table with 5 rows for second and no table should display for the third product. How do I do it?
Can a report be created without a .jasper or .jarxml file?

jitendra
2008-03-03

I am using ireport tool to generate a pdf. In DB i have one column named skills containing values say x,y,z.... in a vertical order. Now when i m generating the report and fetch these values it appears something like this

SKILLS

x
y
z
.
.
.



And i want to make them appeared this way.....

SKILLS : x,y,z,.........


I tried everything i can, even i tried edit->report properties->more tabs, even it doesnt work. Can u gave me an idea how to do that without need to design a new application ??? Plz make it fast, i need it urgently. Thnx

jitendra
2008-03-03

I am using ireport tool to generate a pdf. In DB i have one column named skills containing values say x,y,z.... in a vertical order. Now when i m generating the report and fetch these values it appears something like this

SKILLS

x
y
z
.
.
.



And i want to make them appeared this way.....

SKILLS : x,y,z,.........


I tried everything i can, even i tried edit->report properties->more tabs, even it doesnt work. Can u gave me an idea how to do that without need to design a new application ??? Plz make it fast, i need it urgently. Thnx

jitendra
2008-03-03

I am using ireport tool to generate a pdf. In DB i have one column named skills containing values say x,y,z.... in a vertical order. Now when i m generating the report and fetch these values it appears something like this

SKILLS

x
y
z
.
.
.



And i want to make them appeared this way.....

SKILLS : x,y,z,.........


I tried everything i can, even i tried edit->report properties->more tabs, even it doesnt work. Can u gave me an idea how to do that without need to design a new application ??? Plz make it fast, i need it urgently. Thnx

leoyc
2008-03-21

How can I use mysql data?

Bala
2008-05-13

--------------------------------------------------------------------------------
Hi,
I have a java script file for "ExpndAll", "CollapseAll" functionolities .
I want generate the HTML reports via jasper reports.how to include that java script file into my "jrxml" file.

Thanks in Advance
Bala

Pete
2008-05-21

Reply to:
SKILLS

x
y
z
.
.
.
And i want to make them appeared this way.....

SKILLS : x,y,z,.........

Look at subreport with cross tab. I got it working for images
1,2,3,4,5

instead of

1,
2,
3,
4,
5

Hope that helps.

Stalin
2 days ago

In japser when we try to get the report in Excel format, then it asks where to save. But when we try with pdf option it directly open in the window. what can be done, so that for pdf format also it behaves the same way as Excel

 
 
Leave a Reply
 
Name (required)
Mail (will not be published)(required)
Website
(Case Insensitive)
Bold textItalic textUnderLine textSource CodeHorizontal rulerExternal Link
SourceForge.net