将JSON字符串从Spring控制器传递给JasperReports - 想想我已经接近了

时间:2014-08-26 20:13:09

标签: java json spring spring-mvc jasper-reports

更新 - 通过删除报表中的defaultdataadapter属性,我可以在传递dataSource时显示报告(没有数据)。所以现在我认为JSON正在进入报告,但我可能没有正确使用它。当我将相同的JSON作为字符串而不是json文件传递时,我是否需要更改?如果您对我将错误的JSON数据传递给我的报告有什么错误或者在我的报告中正确使用它,请告诉我。任何帮助将非常感谢!!

==

我一直在尝试通过在控制器中向其传递JSON字符串来获得简单的 JasperReports 。由于我正在为我的Spring MVC应用程序使用Java风格的配置,我添加了这个视图解析器:

@Bean
public ResourceBundleViewResolver getResourceBundleViewResolver(){
  ResourceBundleViewResolver resolver = new ResourceBundleViewResolver();       
  resolver.setBasename("jasper-views");
  resolver.setOrder(0);
  return resolver;
}

然后我用以下内容创建了一个jasper-views.properties文件:

myReport.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
myReport.url=classpath:reports/test_report.jrxml
myReport.reportDataKey=dataSource

现在,当我从控制器返回“myReport”时,它会处理报告。这是我在控制器中所做的事情:

@Controller
public class ReportingController {
    @RequestMapping(value = "/testReport", method = RequestMethod.GET)
    public String getReport(ModelMap modelMap) throws Exception     
    {
        //using mongo's BasicDBObject for easy JSON creation
        BasicDBObject reportingData = new BasicDBObject();
        reportingData.put("name","value");
        BasicDBObject jsonObject = new BasicDBObject("reportingData", JSON.serialize(reportingData));
        JsonDataSource dataSource = new JsonDataSource(new ByteArrayInputStream(JSON.serialize(jsonObject).getBytes("UTF-8")), "reportingData");
        modelMap.put("dataSource", dataSource);
        modelMap.addAttribute("format", "html"); 
        return "myReport";
    }   
}

如果我没有将“reportingData”作为selectExpression(构造函数中的第二个参数传递给JsonDataSource),它会正确呈现报表 - 没有动态元素。如果我传递“reportingData”,则报告为空。根据我的阅读,如果数据源未正确传递,它将为空白。但看起来JsonDataSource对象正在正确填充数据(从调试中检查它)。如果您对我做错了什么有任何建议,请告诉我。

更新 - 如果有帮助,这是我的报告内容。

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="basic_reporting_main" pageWidth="612" pageHeight="792" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="c86cc935-c657-48a6-8972-c27e4c0ebbf8">
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="IQReportingJSON"/>
    <queryString language="json">
        <![CDATA[reportingData]]>
    </queryString>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[name]]></fieldDescription>
    </field>
    <title>
        <band splitType="Stretch"/>
    </title>
    <pageHeader>
        <band splitType="Stretch"/>
    </pageHeader>
    <detail>
        <band height="50" splitType="Stretch">
            <textField>
                <reportElement x="10" y="10" width="200" height="30" forecolor="#0B9E9E" uuid="08154c9f-5698-407e-8646-1bdebd3221c1"/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <staticText>
                <reportElement x="320" y="15" width="100" height="20" uuid="96a8068d-0595-4736-890b-c796dbc8f070"/>
                <text><![CDATA[Static Text]]></text>
            </staticText>
        </band>
    </detail>
    <pageFooter>
        <band splitType="Stretch"/>
    </pageFooter>
</jasperReport>

0 个答案:

没有答案