views:

22

answers:

1

Hi,

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....&gt;

<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?

+1  A: 

Hello Again Mark,

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);
Gordon
@Gordon.. Thanks thanks again for your help. Really appreciate it =). I will try your suggestion later. I just need to sleep for a couple of hours. I will let you know later the result, after I have tried this. Thanks again Sir...
Mark Estrada
@Gordon. Have already tested this and it works like a charm... Thanks again for your help.. =)
Mark Estrada
@Mark, no problem at all. Glad to help.
Gordon