struts2-jasperreports-plugin chart images missing

Hello all,

i'm using the jasperreports-plugin for struts2.

I've created some reports using iReport. When i export a report in any format other than HTML, the report is correctly generated. But when i try to export it in HTML, the browser doesn't find any image.

For reports without charts it isn't a big problem, in them a "px" image is missing. Since it is a static image it is not a great problem, i found a quick workaround and maybe later i will try to force the export parameters to not use images to fill spaces.

My real problem is when i try to generate reports containing charts. The browser doesn't find any of the generated images.

I would like to know what is going wrong, or at least if and where the generated images are stored in a temp folder.

 

Here is the action definition in my struts.xml:

 

 

    <package name="jasperreport" namespace="/reports" extends="jasperreports-default">
          <action name="myJasperTest" class="[...]JasperReportAction" method="getTestReport">
              <result name="success" type="jasper" >
                <param name="location">/WEB-INF/jasper/${outFile}</param>
                 <param name="connection">sqlConnection</param> 
                 <param name="reportParameters">reportParams</param>
                <param name="format">${format}</param>
            </result>
          </action>
    </package>
 
And my Action JasperReportAction.java:
 
    public class JasperReportAction extends ActionSupport {
        //various initialization..getter, setters..
 
        public String getTestReport() {
            try {
                JasperCompileManager.compileReportToFile(sourceFile, outFile);
            
                 //test to see a generated file
                JasperReport report = JasperCompileManager.compileReport(sourceFile);
                JasperPrint print = JasperFillManager.fillReport(report, new HashMap<String, Object>(), sqlConnection);
                JasperExportManager.exportReportToHtmlFile(print, "/home/user/output.html");
             } catch (Exception e) {
                e.printStackTrace();
                return ERROR;
             }
            return SUCCESS;
        }
    }
 
I also have added an action in my struts.xml to avoid exceptions for action not found when the generated report requests url for images like "/images/px" without exension:
    <package name="px" namespace="/images" extends="struts-default">   
        <action name="*">   
            <result>/images/{1}</result>   
        </action>   
    </package>  
 
At least with this i get a "404 - not found" instead of an action not found exception.
The last code rows in the JasperReportAction class creates an HTML file with an associated folder with the generated charts, and all is displayed as it should.
 
I would like to know where i can find the generater charts, or if i can specify a name for them.
Any help would be appreciated :)
 
thanks in advance
giocarmine's picture
Joined: Jun 19 2013 - 7:49am
Last seen: 7 years 3 days ago

1 Answer:

 

I finally solved my problem, mixing the various information I found online and expecially understanding better how to configure struts2.

Jasper Reports uses a servlet called ImageServlet to handle images, so what i needed to do was:

  1. Register Jasper Reports ImageServlet in Tomcat
  2. Configure Struts to not map requests to ImageServlet into actions
  3. Modify the action in struts.xml to make it use ImageServlet

Register the ImageServlet (in Tomcat's web.xml)

 

    <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
    </servlet>
 
    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/servlets/image</url-pattern>
    </servlet-mapping>

 

 

Configure Struts (in struts.xml)

    <constant name="struts.action.excludePattern" value="/servlets/image*" />

 

Modify Action (in struts.xml)

 

<package name="jasperreport" namespace="/reports" extends="jasperreports-default">
 
      <action name="myJasperTest" class="[...]JasperReportAction" method="getTestReport">
          <result name="success" type="jasper" >
             <param name="location">/WEB-INF/jasper/${outFile}</param>
             <param name="connection">sqlConnection</param> 
             <param name="reportParameters">reportParams</param>
            <param name="format">${format}</param>
 
            <param name="imageServletUrl">/servlets/image?image=</param>
        </result>
      </action>
    </package>
 

There was no need for the package "px" in struts.xml I added at first.

giocarmine's picture
Joined: Jun 19 2013 - 7:49am
Last seen: 7 years 3 days ago
Feedback
randomness