Friday, April 8, 2011

Jasper Report with embedded SQL using Struts2 + Spring Framework

We have decided that in order to make our Jasper Reports portable we need to start embedding SQL queries in our JasperReports. The plan is for all future Jasper Reports with embedded SQL queries be deployed to a dedicated JasperServer. This way the applications do not hang when someone runs a huge report; this will prevent hair being pulled out of frustration.

Our environment uses Struts 2-Spring-Hibernate + Jasper for reporting. The challenge is how to pass the connection to the JasperReports.


So here is the solution I came up with:


1. Make sure that the dataSource is declared in Spring.



2. Add a DataSource variable to the Action. In this example I autowired the datasource; it can also be mapped declaratively. 


3. Add a Connection variable in the Action and a getter. The connection will be set when the method that will return the Report is called.


4. In the struts.xml, provide the connection parameter name with the name of the connection variable in the action class. 








1 comment: