Jasper Report 6.3超链接扩展

时间:2016-09-12 05:23:20

标签: jasper-reports

我最近将Jasper Report API从4.5.1升级到6.3。使用4.5.1,报告将导出为HTML,PDF格式。对于HTML报告,可以深入查看子报告。为了自定义处理请求参数的链接和打开子报告的预处理已创建ExtensionRegistryFactory并注册JRHyperlinkProducerMapFactory以处理超链接。

我注意到扩展程序已正确注册但未被使用。检查Jasper Report 6.3的源代码并尝试调试原因,然后观察该方法:net.sf.jasperreports.engine.JRAbstractExporter.getHyperlinkProducer(JRPrintHyperlink)不返回JRHyperlinkProducer

以下是ExtensionsRegistryFactory的代码:

public class HyperlinkExtensionsRegistryFactory implements ExtensionsRegistryFactory
{
    @Override
    public ExtensionsRegistry createRegistry(String registryId, JRPropertiesMap properties)
    {
        return new ExtensionsRegistry()
        {
            @Override
            public List getExtensions(Class extensionType)
            {
                if (extensionType.equals(JRHyperlinkProducerFactory.class))
                {
                    JRHyperlinkProducerMapFactory producerFactory = new JRHyperlinkProducerMapFactory();
                    producerFactory.addProducer("ReportExecution", new RemoteExecutionHyperlinkProducer());
                    producerFactory.addProducer("Custom", new ExpandCollapseHyperlinkProducer());
                    return Arrays.asList(producerFactory);
                }
                return null;
            }
        };
    }

    public static class RemoteExecutionHyperlinkProducer implements JRHyperlinkProducer
    {
        @Override
        public String getHyperlink(JRPrintHyperlink hyperlink)
        {
            return [custom link generation logic];
        }
    }

    public static class ExpandCollapseHyperlinkProducer implements JRHyperlinkProducer
    {
        @Override
        public String getHyperlink(JRPrintHyperlink hyperlink)
        {
            return [custom link generation logic];
        }
    }
}

使用此类,已为jasperreports_extension.properties文件创建了一个条目。以下是其内容:

net.sf.jasperreports.extension.registry.factory.HyperlinkExtensionFactory=<fully_qualified_path_to_HyperlinkExtensionsRegistryFactory>

我错过了什么吗?如果我有任何错误,那么请帮助找出那个。

1 个答案:

答案 0 :(得分:0)

感谢Narcis提供的意见。

我已检查过Jasper Report 6.3.0的源代码,并试图了解如何替换Hyperlink注册表。找到了解决这个问题的方法。希望它对别人有所帮助。这是注册超链接扩展的代码片段。

<强>导出

AbstractHtmlExporter<HtmlReportConfiguration,HtmlExporterConfiguration> exporter = new HtmlExporter();

SimpleHtmlReportConfiguration htmlReportConfig = new SimpleHtmlReportConfiguration();

htmlReportConfig.setHyperlinkProducerFactory(HyperlinkExtensionsRegistryFactory.hyperlinkProducerFactory());

exporter.setConfiguration(htmlReportConfig);

<强> HyperlinkExtensionsRegistryFactory.hyperlinkProducerFactory()

public JRHyperlinkProducerFactory hyperlinkProducerFactory() {
    JRHyperlinkProducerMapFactory producerFactory = new JRHyperlinkProducerMapFactory();
    producerFactory.addProducer("ReportExecution", new <Class_implements_JRHyperlinkProducer>());
    producerFactory.addProducer("Custom", new <Class_implements_JRHyperlinkProducer>());
    return producerFactory;
}