Recently I spent a whole day searching for solutions and experimenting with solutions for how to pass additional report parameters to a Jasper Report from the Struts 2 framework. It took 8 hours because I had to piece together the information that I needed from multiple locations and then I had to experiment and test the solutions in my application.
Therefore, I am taking what I have learned and presenting it here so that others do not have to a waste a similar amount of time on this in the future.
Step1: Configuring the Struts2 JasperReports Plug-in
The Struts2 JasperReports plug-in makes integrating JasperReports into your application simple and even enjoyable. The following shows how to configure a report using this plug-in.
<result-types>
<result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult" default="false"/>
</result-types>
<action name="viewShortReport" class="actions.ShortReport" method="eventsByRating">
<result name="success" type="jasper">
<param name="location">reports/short_report.jasper</param>
<param name="dataSource">events</param>
<param name="format">PDF</param>
</result>
</action>
To pass a Map of additional report parameters to the report you need to include an additional "param name" called "reportParameters" tag within the "result" tag. The value for this tag should be the name of the Map holding the keys and values for the report parameters in your Action, in my case I called the Map "reportParams".
<action name="viewShortReport" class="actions.ShortReport" method="eventsByRating">
<result name="success" type="jasper">
<param name="location">reports/short_report.jasper</param>
<param name="dataSource">events</param>
<param name="format">PDF</param>
<param name="reportParameters">reportParams</param>
</result>
</action>
Step 2: Modify the Report Action
Now go to your Action and expose a getter for the Map that you specified in Step 1. After I made my changes the following code was added to my Action to expose that getter. ** Note: Of course you will need to do the heavy lifting within the heart of the Action to populate the Map with the keys and values that you want passed to the report.
private HashMap reportParams = new HashMap();
public HashMap getReportParams() {
return reportParams;
}
public String eventsByRating() throws Exception {
reportParams.put("sessionName", session.getSessionName());
events = Event.getEventsBySessionIdOrderByRating(sessionId);
...
return SUCCESS;
}
Step 3: Use the Parameters in the Report
As you saw in Step 2 I added a parameter to the Map called "sessionName". Next I need to go into my report and modify the report to get this value out of the parameter map and into the report for display. Here is the XML that I added to my report to make the value for this parameter available to body of the report.
<parameter name="reportParams.sessionName" isforprompting="false" class="java.lang.String"></parameter>
I sincerely hope that this post helps others short circuit the research required to make this happen. Feel free to post a comment or contact me about if you have additional questions or if you experience problems using this advice.
分享到:
相关推荐
Java Project for reporting using jasper report + struts2
Jasper report用户手册Jasper report用户手册Jasper report用户手册Jasper report用户手册
碧玉报告示例 PDF(俄文字体)、word、excel报告示例。
jasper报表(odoo)
jpa-struts2-碧玉 Struts 2 + JPA + jasperreports插件5.6.1 描述 碧玉与Struts的整合2。 用 Techno:JasperReports 5.6,Struts 2,JPA,Springs。 BD:PostGreSQL(基础:struts2,postgres:管理员)。 链接:
这张幻灯片说明了如何使用SpringMVC,Jasper Report,Struts2MVC和更多技巧
Jasper Report用户手册 version1.0 1 简介 2 API概览 Class net.sf.jasper.engine.design.JasperDesign Class net.sf.jasper.engine.JasperReport Class net.sf.jasper.engine.JasperCompileManager Class ...
春天碧玉的报告如何使用Spring Boot和Jasper生成动态报告下载JasperSoft Studio工具链接: : //community.jaspersoft.com/project/jaspersoft-studio/releases
使用ireport和struts2进行报表设计的简单示例,生成普通报表和饼图、柱状图。java工程的lib里包含丰富的jar包,且很多都是从各个网站下载的最新版本,文件夹内还包含一些ireport和jasperreport文档,适合刚入门学习...
关于Jasper Report培训的PPT文档(英文)。
jasper-soap-api-tools 适用于中国联通的Jasper Soap API工具。
jasper-yelp 模块 的 Yelp 模块 示例对话: 碧玉 给我找一家餐厅 什么样的美食? 意大利语 附近? 波士顿 伟大的。 在波士顿寻找意大利餐馆 Tileston & Clark St 的这个地方怎么样?它被称为 Giacomo's ...
jasper-report-helper 是一个项目,旨在帮助您以一种简单的方式生成 OpenNMS 的所有报告,而无需使用 OpenNMS UI。 将所有报告和子报告从 OpenNMS 复制到 src/main/resources/report-templates。 目录结构应与 ...
maven struts2 jasperreport ireport亲测通过整合多家资源
struts2+ireport集成包,解决最新版本的struts2下报表开发时报表会报错的情况
org.apache.jasper.JasperException: Template /template/ajax/text.ftl not found异常解决包
Jasper Report
碧玉节点服务器在NodeJS中托管Jasper报告下载git clone git:github.com/andrewloable/jasper-node-server.gitnpm install图书馆将所有Jasper ... { "reports":{ "test1": { "jrxml": "/jrxml/test.jrxml" }, "test2
按书上写了一小JAVABEAN的报表,
jasper server api but not jasper report api