i'm using the struts2 jasperreports-plugin and I'm trying to generate an HTML report invoking an action.
The .jasper file is generated using iReport.
I successfully generated the report using a List as datasource, but i'm getting some problems trying to use a jdbc connection.
This is what i've done following the tutorial.
public class JasperReportAction extends ActionSupport {
private List<User> users;
//getters and setters
public String getTestReport() {
User u1 = new User();
User u2 = new User();
setSomeParameters(u1, u2);
users.add(u1);
users.add(u2);
return SUCCESS;
}
}
And this is what i added in my struts.xml file:
<package name="jasperreport" namespace="/reports" extends="jasperreports-default">
<action name="myJasperTest" class="[...]JasperReportAction" method="getTestReport">
<result name="success" type="jasper" >
<param name="location">report2.jasper</param>
<param name="dataSource">users</param>
<param name="format">HTML</param>
</result>
</action>
</package>
And this works. I have my report with the data defined in the list. I've tried to modify my action to establish a jdbc connection, retrieving the data with a simple "select" query:
public class JasperReportAction extends ActionSupport {
private java.sql.Connection sqlConnection;
//getters and setters..
public String getTestReport() {
try {
sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pwd");
}
catch(Exception e) {
return ERROR;
}
return SUCCESS;
}
}
And in the struts.xml I changed the datasource to "sqlConnection". As a result i get a report with only one row with null values.
I also tried to generate an output file using the same connection:
public String getTestReport() {
try {
sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "user", "pwd");
JasperReport report = JasperCompileManager.compileReport("report2.jrxml");
JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), sqlConnection);
JasperExportManager.exportReportToHtmlFile(print, "repo.html");
}
catch(Exception e) {
return ERROR;
}
return SUCCESS;
}
When I invoke the action, the displayed report still contains null values, but the generated html file contains all the data stored in the table.
I'm new with JasperReports and I don't know if I'm missing something or doing something wrong.
Thanks in advance for any help :)