首页 > 解决方案 > 在运行时更改 jasper 报告参数

问题描述

我知道,但我们真的需要它。我们有明确的分工。他们创建模板,我根据一些规则在运行时填充它们。

$P{risk_types}.get($F{risk_type}) ?: "未定义"

SO:需要在运行时替换参数的内容(也是默认的)。示例:需要设置 JSON_INPUT_STREAM

就像这个未解决的线程。 https://community.jaspersoft.com/questions/516611/changeing-parameter-scriptlet 真的希望不在xml级别上工作,但据我尝试,xml也无法解决我的问题。谢谢!

标签: jasper-reports

解决方案


我们这样做的最简单和最干净的方法(绕过使用大量已弃用的文档和未完成的错误未记录的静态反模式新功能):

使用存储库扩展创建上下文

SimpleJasperReportsContext jasperReportsContext = new SimpleJasperReportsContext();
jasperReportsContext.setExtensions(RepositoryService.class, Collections.singletonList(new MyRepositoryService(jasperReportsContext, yourOptionalParams)));

以这种方式填写(在编译和其他常用操作之后)

JasperPrint print = JasperFillManager.getInstance(jasperReportsContext).fill(compiled, new HashMap<>());

现在,您的存储库必须扩展默认的存储库才能成功进行 hack 注入(因为 hodgie 编码为“isAssignableFrom”)

public class PrintFormsRepositoryService extends DefaultRepositoryService {

    @Override
    public InputStream getInputStream(RepositoryContext context, String uri) {
        // return here your own good simple poj inputStream even from memory if you found source
        // or pass to another repository service(default one probably)
        return null;
    }

}

推荐阅读