2
7/31/2019 Jasper Reports Using Struts2 http://slidepdf.com/reader/full/jasper-reports-using-struts2 1/2 Jasper Reports using Struts2: Report Parameters 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. <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>

Jasper Reports Using Struts2

Embed Size (px)

Citation preview

Page 1: Jasper Reports Using Struts2

7/31/2019 Jasper Reports Using Struts2

http://slidepdf.com/reader/full/jasper-reports-using-struts2 1/2

Jasper Reports using Struts2: Report ParametersRecently I spent a whole day searching for solutions and experimenting with solutions forhow to pass additional report parameters to a Jasper Report from the Struts 2 framework. Ittook 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 haveto 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 applicationsimple 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 anadditional "param name" called "reportParameters" tag within the "result" tag. The value forthis tag should be the name of the Map holding the keys and values for the reportparameters 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>

Page 2: Jasper Reports Using Struts2

7/31/2019 Jasper Reports Using Struts2

http://slidepdf.com/reader/full/jasper-reports-using-struts2 2/2

<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 Imade 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 topopulate 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 togo into my report and modify the report to get this value out of the parameter map and intothe report for display. Here is the XML that I added to my report to make the value for thisparameter 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 thishappen. Feel free to post a comment or contact me about if you have additional questionsor if you experience problems using this advice.