Recently I spent a whole day searching for solutions and experimenting with solutions for how to pass additional report parameters to a Jasper Report from the Struts 2 framework. It took 8 hours because I had to piece together the information that I needed from multiple locations and then I had to experiment and test the solutions in my application.
Therefore, I am taking what I have learned and presenting it here so that others do not have to a waste a similar amount of time on this in the future.
Step 1: Configuring the Struts2 JasperReports Plug-in
The Struts2 JasperReports plug-in makes integrating JasperReports into your application simple and even enjoyable. The following shows how to configure a report using this plug-in.
To pass a Map of additional report parameters to the report you need to include an additional "param name" called "reportParameters" tag within the "result" tag. The value for this tag should be the name of the Map holding the keys and values for the report parameters in your Action, in my case I called the Map "reportParams".
Step 2: Modify the Report Action
Now go to your Action and expose a getter for the Map that you specified in Step 1. After I made my changes the following code was added to my Action to expose that getter. ** Note: Of course you will need to do the heavy lifting within the heart of the Action to populate the Map with the keys and values that you want passed to the report.
Step 3: Use the Parameters in the Report
As you saw in Step 2 I added a parameter to the Map called "sessionName". Next I need to go into my report and modify the report to get this value out of the parameter map and into the report for display. Here is the XML that I added to my report to make the value for this parameter available to body of the report.
I sincerely hope that this post helps others short circuit the research required to make this happen. Feel free to post a comment or contact me about if you have additional questions or if you experience problems using this advice.
Therefore, I am taking what I have learned and presenting it here so that others do not have to a waste a similar amount of time on this in the future.
Step 1: Configuring the Struts2 JasperReports Plug-in
The Struts2 JasperReports plug-in makes integrating JasperReports into your application simple and even enjoyable. The following shows how to configure a report using this plug-in.
<result-types>
<result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult" default="false"/>
</result-types>
<action name="viewShortReport" class="actions.ShortReport" method="eventsByRating">
<result name="success" type="jasper">
<param name="location">reports/short_report.jasper</param>
<param name="dataSource">events</param>
<param name="format">PDF</param>
</result>
</action>
To pass a Map of additional report parameters to the report you need to include an additional "param name" called "reportParameters" tag within the "result" tag. The value for this tag should be the name of the Map holding the keys and values for the report parameters in your Action, in my case I called the Map "reportParams".
<action name="viewShortReport" class="actions.ShortReport" method="eventsByRating">
<result name="success" type="jasper">
<param name="location">reports/short_report.jasper</param>
<param name="dataSource">events</param>
<param name="format">PDF</param>
<param name="reportParameters">reportParams</param>
</result>
</action>
Step 2: Modify the Report Action
Now go to your Action and expose a getter for the Map that you specified in Step 1. After I made my changes the following code was added to my Action to expose that getter. ** Note: Of course you will need to do the heavy lifting within the heart of the Action to populate the Map with the keys and values that you want passed to the report.
private HashMap reportParams = new HashMap();
public HashMap getReportParams() {
return reportParams;
}
public String eventsByRating() throws Exception {
reportParams.put("sessionName", session.getSessionName());
events = Event.getEventsBySessionIdOrderByRating(sessionId);
...
return SUCCESS;
}
Step 3: Use the Parameters in the Report
As you saw in Step 2 I added a parameter to the Map called "sessionName". Next I need to go into my report and modify the report to get this value out of the parameter map and into the report for display. Here is the XML that I added to my report to make the value for this parameter available to body of the report.
<parameter name="reportParams.sessionName" isForPrompting="false" class="java.lang.String"/>
I sincerely hope that this post helps others short circuit the research required to make this happen. Feel free to post a comment or contact me about if you have additional questions or if you experience problems using this advice.


18 comments:
John - I've just been given this exact task. Thank you for taking the time to post this!
I will be implementing your advice this week and will post back with any additional thoughts or questions.
--JohnB
hey john,
your tutorial was very useful for me.But i have a problem,I don't want the pdf to be opened on complete page.I want to open a download box.
How can i implement?
Thankyou,
ravi
For open a download box, please use the param :
(param name="contentDisposition")attachment;filename="YourReportName.pdf"(/param)
( ) means < >
Thank's
Regards
Chandra (Indonesia)
Thank you dear friend!!!
I am your spanish friend forever.
by ErRubio
Hi John... I'd spent more than 80 hours doing research on how to enforce Map params to jasper reports shooting from struts2 frame work.. Your 8 hours of effort will defenitely going to help devolopers around the world..
God bless you..
KP Uthaman Belgaum
Hi,
Thanks for your tutorial. I have question about what if I want to print the PDF to printer without showing it on the page. What should I do?
JJ
very useful. helped me a lot!
Hi jhon,
thank for your tutorial.
I did this step in my application and it's work.
But, when i replace the format from PDF to XLS or CSV, it doesn't work. please advice me what should I do?
Rgds,
Alex
Hi
Please tell me how to display the Jasper Reports as part of a JSP
Dear All,
One Main Jasper Report.That report contains two supreport.I want pass parameter value one subreport to another subreport.I can pass parameter main report to subreport through subreportParameterExpression.please help me.I am waiting for your reply.Advance thanks to all.
Thanks & Regards
Siva
Hi John,
Thanks for your tutorial. I have a question.
I want to change the format and location parameters from my Action.
How can I implement?
Thankyou
Sonia
hai,
I want more than one pojo class object as datasource. But i dont how to do this. Can you give any suggestion for me.
Ex: I have two classes: class A and class B. a is the object of A. b is the of B. I wanna add these two for
datasource.
We use Struts2, Spring and Hibernate in our projects. There are two ways we do Jasper Reports and they both use the Struts2-Jasper plugin
Use Hiberate to retrieve the List of objects to be put in the report. This technique we try avoid as much as possible as the report is not portable and forces the report to be generated in the same JVM as your application.
We use embedded SQL in the JRXML. This we think is a better approach because eventually we can upload the JRXML to a dedicated JasperServer. Prior to Struts 2.1.x this was difficult to implement because there is no way to pass the connection to the JRXML. The following is the tutorial as to how to integrate Jasper Report with embedded SQL with Struts 2
http://yellow-jbox.blogspot.com/2011/04/jasper-report-with-embedded-sql-using.html
Thanks John for this wonderful tutorial.
I just added one hash map with the report parameters in the action and added setter/getter for it.
Also in the struts.xml file just mentioned <param> name="reportParameters">reportParameters</param>
Only thing I want to mention here is that you dont need to specify
"reportParams.sessionName" just will do as shown below.
May be many would have already noticed that but thought I will just mention it.
Thanks.
dk
parameter name="sessionName"
Thank you. Pointed me in the right direction for including images in my reports and using annotations.
Thank you!!! After 3 days i finally found your useful tutorial! :)
Thanks for sharing your knowledge. No need for others to reinvent the wheel.
Post a Comment