Mark Estrada
Mark Estrada

Reputation: 9191

Add Parameter Map on Jasperreport at Spring MVC

I have a simple jasper report that does not need a datasource so I use a JREmptyDataSource. It only relies on a parameter map that is being used to fill the report

<?xml version="1.0"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net....>

<parameter name="param1" class="java.lang.String"/>
<parameter name="param2" class="java.lang.String"/>
    <detail>
        <band height="35">
            <staticText>
                <reportElement x="20" y="0" width="115" height="30"/>
                <text>
                    <![CDATA[Parameter Values:]]>
                </text>
            </staticText>
            <textField>
                <reportElement x="135" y="11" width="100" height="19"/>
                <textFieldExpression>
                    <![CDATA[$P{param1}]]>
                </textFieldExpression>
            </textField>
            <textField>
                <reportElement x="250" y="11" width="100" height="19"/>
                <textFieldExpression>
                    <![CDATA[$P{param2}]]>
                </textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

My problem is, I cant find a link on how to send the parameter map in my spring model controller. For the datasource, according to the spring docs, just add the datasource attribute on the model map which I did already and then add the object JREmptyDataSource

But how about the parameter map? What attribute name can I used so that it will fill my report accordingly?

public ModelAndView generateReport(HttpServletRequest request,
    HttpServletResponse response) {
    Map model = new HashMap();

    model.put("datasource", new JREmptyDataSource());
    //how to send the parameter map?  

    return new ModelAndView("report", model);
}

In usual filling using only a servlet, I found a resource on the net that does like this. How can I send the parameter map in spring mvc case?

protected void doGet(HttpServletRequest request, HttpServletResponse
    response)throws ServletException, IOException
{
    ServletOutputStream servletOutputStream = response.getOutputStream();
    InputStream reportStream = getServletConfig().getServletContext()
        .getResourceAsStream("/reports/reports.jasper");
    HashMap parameterMap = new HashMap();
    parameterMap.put("paramName", "paramValue");
    try
    {
        JasperRunManager.runReportToPdfStream(reportStream,
            servletOutputStream, parameterMap, new JREmptyDataSource());
        response.setContentType("application/pdf");
        servletOutputStream.flush();
        servletOutputStream.close();
    }catch(Exception e){
    }
}

Any thoughts please?

Upvotes: 3

Views: 9869

Answers (1)

Gordon
Gordon

Reputation: 4843

From looking at this page you treat the hashmap you use to pass the datasource as the parameter map adding the parameters as normal and pass the map as below.

Map model = new HashMap();
model.put("paramName", paramValue);
return new ModelAndView("report.name", model);

Upvotes: 2

Related Questions