Add parameter map to Jasperreport in Spring MVC

I have a simple jasper report that does not need a data source, so I use JREmptyDataSource. It uses only the parameter map, which is used to populate 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 that I cannot find a link on how to send the parameter map to my spring model controller. For the data source, according to spring docs, just add the datasource attribute to the model map I already made, and then add the JREmptyDataSource object

But what about a parameter map? What attribute name can I use to populate my report?

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 the usual filling, using only the servlet, I found a resource on the network that does this. How can I send a parameter map in case of spring mvc?

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?

+3
1

, , .

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

Source: https://habr.com/ru/post/1763040/


All Articles