Jerry Jacob's Blog

Render Charts in Jasper HTML using Spring

Posted in Java/ J2EE, Uncategorized by jerlinworld on January 13, 2014

This is a solution for which I worked for 2 full days searching Google and trying it out by trial and error. The main issue that I was facing was that the charts in my report won’t render in the HTML report, while it was rendering properly in the PDF version.
I would assume that you know how to setup Jasper using Spring. You have to do 4 things to make the graphs appear in your HTML.
1. A new servlet mapping in your web.xml

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

2. Set the IMAGES_URI parameter in your jasper-view.xml (file where you define your jasper views). The URI path should match the servlet path you defined in web.xml

<util:map id="exportParameterMap">
	         <entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN">
	                 <value>false</value>
	         </entry>
	         <entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND">
	                 <value>false</value>
	         </entry> 
	          <entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI">
	             <value>image?image=</value>
	          </entry> 
	</util:map>
	
    	<bean id="roleUsageReport"
          class="com.jerry.util.SpReportsMultiFormatView"
          p:url="/resources/shared/reports/usageAnalytics.jasper"
          p:reportDataKey="datasource"
          p:exporterParameters-ref="exportParameterMap"/>

3. Extend the JasperReportsMultiFormatView and override the renderReport method as shown below. In this case, I named it com.jerry.util.SpReportsMultiFormatView as you can see in the above xml.

	public class SpReportsMultiFormatView extends JasperReportsMultiFormatView {

	@Override
	protected void renderReport(JasperPrint populatedReport,
			Map<String, Object> model, HttpServletResponse response)
			throws Exception {
		if (model.containsKey("requestObject")) {
		      HttpServletRequest request = (HttpServletRequest) model.get("requestObject");
		      request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, populatedReport);
		}
		super.renderReport(populatedReport, model, response);
	}

}

4. In your spring controller, set the ‘requestObject’ in your model.

	JRDataSource JRdataSource = new JRBeanCollectionDataSource(usageList);
	Map<String,Object> parameterMap = new HashMap<String,Object>();
	parameterMap.put("format", "html");
	parameterMap.put("requestObject", req);
        parameterMap.put("datasource", JRdataSource);
        return new ModelAndView("roleUsageReport", parameterMap);

This changes should make the graphs/images render properly in your HTML report.

Advertisements