Monday, March 13, 2006

How to change your subreport paths at runtime?

See the "How does Jasper subreporting works?" article above in order to understand this article. If you use ireports, you can double click the sub-reports block and specify your subreport name. If you select the image expression class as "java.lang.String", you can specify your subreport name(compiled) as a java string. You can specify say mysubreport.jasper and expect the jasper file to be in the folder. You can also specify an absolute path for your sub report. While programming it is best to reduce the dependency on such hardcoded paths in the report template.

Here is the solution:
While creating the report template, always use the filename for your subreport name. At run time, replace this filename with your sub-report absolute path location which can be fetched from a property file. Code below, does exactly the same!

private void changeSubReportPath(JasperDesign jasperDesign)
{
Object[] objExpressions = jasperDesign.getExpressions().toArray();
for (int i=0; i
{
JRDesignExpression jrExpression = (JRDesignExpression)objExpressions[i];
String expressionText = jrExpression.getText();
if ((expressionText.indexOf(".class") != -1) || (expressionText.indexOf(".jasper") != -1))
{
jrExpression.setText("\"" + System.getProperty("report.child.basepath") + expressionText.substring(1));

}

}
}

0 Comments:

Post a Comment

<< Home