Jerry Jacob's Blog

Render Charts in Jasper HTML using Spring

Posted in Java/ J2EE, Uncategorized by Jerry Jacob 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


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">
	         <entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND">
	          <entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.IMAGES_URI">
    	<bean id="roleUsageReport"

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 {

	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.


One Response

Subscribe to comments with RSS.

  1. praveen said, on April 2, 2014 at 1:21 am

    Can you give give details about the Request Object you are setting and what about the Jasper Print object ….how are you populating

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: