Struts2向JasperReport传参数配置
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 .
最近,我花了一整天的时间来寻找和测试如何通过 Struts2向JasperReport传递额外参数的方案。我从多方收集资料,然后在我自己的应用程序里进行测试,为此花了8个小时来拼凑这些信息。
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 .
步骤1:配置Struts2配置文件以支持 JasperReports插件
Struts2的JasperReports插件可以在你的应用程序里制作简单而又有趣的综合报表。下面的代码将向你展示如何配置JasperReports插件与Struts2协同工作 。
译者注: JasperReports 插件在Struts2的完整压缩包里,包名是 struts2-jasperreports-plugin-2.x.x.jar,将其拷贝到WEB项目工程的CLASSPATH下,即 “WebContent\WEB-INF\lib”下。
Struts关于JasperReport的部分配置XML代码:
要想通过MAP方法向报表中传递一个额外参数,在 struts.xml文件中的“result”标签中需要添加一个名为“reportParameters”的参数名,标签中参数名就是你在Action 中所设置的值,依我看,我认为这是一个匹配参数的操作。
- <package?name="com.olympus.sapg.smtinnovation.action.jasper"?extends="jasperreports-default">
- ?????<result-types>
- ?????????<result-type?name="jasper"?class="org.apache.struts2.views.jasperreports.
- JasperReportsResult"></result-type>
- ?????</result-types>
- ?
- ???? <action?name="pullMaterielBill"?class="com.olympus.sapg.smtinnovation.action.
- jasper.PrintPullMaterielBill">
- ???? <result?name="success"?type="jasper">
- ????????? <param?name="location">jasper\productionPlan\pullMaterielBill.jasper</param>
- ????????? <param?name="format">HTML</param>
- ????????? <param?name="dataSource">pullMaterialList</param>
- ????????? <param?name="reportParameters">Params</param>
- ?????????</result>
- ?????</action>
- </package>?
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.
步骤2:修改报表Action
现在,进入你的Action,为你在步骤1中所期望的 Map数据配置一个getter方法,下面的代码中,将在Action中增加Map的getter方法。
- private?HashMap?Params?=?new?HashMap();
- public?HashMap?getParams()?{
- ?????return?Params;?
- }
- ?
- public?List<PullMaterial>?getPullMaterialList(){
- ?????List<Criterion>?criterionPullMaterial=new?ArrayList<Criterion>();
- ?????List<Criterion>?criterionPullMaterialBill=new?ArrayList<Criterion>();
- ?????List<PullMaterial>?pullMaterials?=?new?ArrayList<PullMaterial>();
- ?
- ?????criterionPullMaterialBill.add(Restrictions.between("billNumber",
- ?????????????????queryBillNumBegin,
- ?????????????????queryBillNumEnd));
- ?
- ?????pullMaterials?=?pullMaterialService.pullMaterialBillMake(
- ?????????????????????Order.asc("billNumber"),
- ?????????????????????Order.asc("materialNumber"),
- ?????????????????????criterionPullMaterial,
- ?????????????????????criterionPullMaterialBill);
- ?
- ?????Params.put("name",?"许亮");
- ?????return?pullMaterials;?
- }??
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 .
步骤3:在报表中使用参数
你也看到了,在步骤2中我增加了一个参数“name” (Params.put("name", "许亮");),接下来,我要进入我的报表,给报表设置一个参数名也为“name”,下面是部分代码:
jxml代码:
- <parameter?name="reportParams.name"?isforprompting="false"?class="java.lang.String">
- </parameter>?