如何在jasper服务器上使用xml数据源 [英] How to use xml data source on jasper server

查看:192
本文介绍了如何在jasper服务器上使用xml数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在jasper服务器(5.0.0)中使用xml数据源。在应用程序运行时动态创建Xml文件,因此不同的报告将具有不同的xml数据源。我知道jasper服务器没有定义XML数据源,但我发现创建没有数据源的报告,然后传递参数XML_FILE - java.io.File将完成工作。我设法在java servlet中执行此操作:

I would like to use xml data source in jasper server (5.0.0). Xml files are created "on the fly" while application is running so different reports will have different xml data sources. I know that jasper server does not have XML data source defined but I found out that creating report without data source and then passing parameter XML_FILE - java.io.File will do the work. I managed to do this in java servlet:

  jasperReport = JasperCompileManager.compileReport("path to jrxml");       
  HashMap map  = new HashMap();
  map.put("XML_FILE", new File(xmlSourceFile));     
  jasperPrint = JasperFillManager.fillReport(jasperReport,map);         
 byte [] o = JasperExportManager.exportReportToPdf(jasperPrint);

但遗憾的是无法在jasper服务器上执行此操作。我正在使用rest服务来运行报告,所以我只能使用String参数。我已经尝试编写一个将String参数与xml url一起转换为java.io.File的scriptlet

but unfortunately failed to do it on jasper server. I am using rest services to run report so I can use only String parameters. I've tried to write a scriptlet that converts String parameter with xml url to java.io.File

public class XmlScriplet extends JRDefaultScriptlet{
 @Override
 public void beforeReportInit(){
  try {
       String param = (String)this.getParameterValue("fileName");
       HashMap map  = new HashMap();
       map.put("XML_FILE", new File(param));
       this.parametersMap.putAll(map);

  } ...

但是这给了我一个空的报告。
提前谢谢。

but this gives me an empty report. Thank you in advance.

推荐答案

您还可以传递任何XML资源(是静态XML甚至是REST)如果您使用的是Jasperreports Server 5.5,并且假设您的报告单位名为report且您的xml数据资源位于<$ c,那么通过简单地将net.sf.jasperreports.xml.source参数传递给您的报告$ c> http://(host):( port)/resource.xml 路径,您可能希望使用Jasper的REST v2 API来调用报告,如下所示:

You can also pass any XML resource (be an static XML or even a REST service) by passing by simply "net.sf.jasperreports.xml.source" parameter to your report, if you are using Jasperreports Server 5.5, and assuming your report unit is named "report" and your xml data resource is in http://(host):(port)/resource.xml path, you might want to call the report using Jasper's REST v2 API like this:

 http://(host):(port)/jasperserver/rest_v2/reports/path/to/your/report/unit/report.html?net.sf.jasperreports.xml.source=http://(host):(port)/resource.xml

为了在报告中使用默认的net.sf.jasperreports.xml.source值,您还应该在报告源jrxml的参数部分添加以下内容

In order to have a default "net.sf.jasperreports.xml.source" value in your report, You should also add the following in the "parameters" section in your report source jrxml

<parameter name="net.sf.jasperreports.xml.source" class="java.lang.String"> 
    <defaultValueExpression><![CDATA["http://(host):(port)/resource.xml"]]>   </defaultValueExpression>
</parameter>

不要忘记通过追加:


Do not forget to add xpath2 query support to jasperreports server by appending:

# addition for xpath2 queries
net.sf.jasperreports.query.executer.factory.xpath2 = net.sf.jasperreports.engine.query.JRXPathQueryExecuterFactory

到部署目录中的/WEB-INF/classes/jasperreports.properties appserver

to /WEB-INF/classes/jasperreports.properties inside the deployed dir in the appserver

有关xml数据源允许的参数的更多信息,您还可以查看官方的文档

For more info about which parameters xml data source allows, you could also take a look at the official documentation

这篇关于如何在jasper服务器上使用xml数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆