jasper-reports - 在运行时更改 jasper 报告参数
问题描述
我知道,但我们真的需要它。我们有明确的分工。他们创建模板,我根据一些规则在运行时填充它们。
- 无法教我的企业插入这样的内容并确保他们确实做到了(因此无法将任何逻辑移至模板):
$P{risk_types}.get($F{risk_type}) ?: "未定义"
- 也无法从由天知道的人编写的某些适配器中硬编码的文件中填充,并且在运行时不可更改。这是一个网络应用程序。最好的选择是找到一种方法将该文件源从适配器替换为 ByteArrayStream。
SO:需要在运行时替换参数的内容(也是默认的)。示例:需要设置 JSON_INPUT_STREAM
就像这个未解决的线程。 https://community.jaspersoft.com/questions/516611/changeing-parameter-scriptlet 真的希望不在xml级别上工作,但据我尝试,xml也无法解决我的问题。谢谢!
解决方案
我们这样做的最简单和最干净的方法(绕过使用大量已弃用的文档和未完成的错误未记录的静态反模式新功能):
使用存储库扩展创建上下文
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;
}
}
推荐阅读
- c - 线程终止后的状态
- c# - 带有 ISampleProvider 的 NAudio WasapiOut 随机停止
- ios - Swift 5 Present ViewController 中途
- python - Python列表没有给出正确的结果
- arrays - Mongodb 使用 $elemMatch 到 $filter
- reactjs - 是否可以在同一个域下使用 Next.js 和 WordPress 页面?
- php - CakePHP 4、AWS Elasticsearch 7 - 请求中包含的安全令牌无效
- hibernate - JPA:使用 JoinTable 删除 OneToMany 关系的所有实体
- database - 如何使用 Oracle 12c 中的 impdp 将带有配置文件的 dmp 文件从 CDB 数据库导入 PDB 数据库实例?
- java - 将数据从活动传递到另一个片段