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代码:

 
  1. <package name="com.olympus.sapg.smtinnovation.action.jasper" extends="jasperreports-default">
  2.      <result-types>
  3.          <result-type name="jasper" class="org.apache.struts2.views.jasperreports.
  4. JasperReportsResult"></result-type>
  5.      </result-types>
  6.  
  7.      <action name="pullMaterielBill" class="com.olympus.sapg.smtinnovation.action.
  8. jasper.PrintPullMaterielBill">
  9.       <result name="success" type="jasper">
  10.           <param name="location">jasper\productionPlan\pullMaterielBill.jasper</param>
  11.           <param name="format">HTML</param>
  12.           <param name="dataSource">pullMaterialList</param>
  13.      </result>
  14.      </action>
  15. </package> 

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".

要想通过MAP方法向报表中传递一个额外参数,在 struts.xml文件中的“result”标签中需要添加一个名为“reportParameters”的参数名,标签中参数名就是你在Action 中所设置的值,依我看,我认为这是一个匹配参数的操作。

 
  1. <package name="com.olympus.sapg.smtinnovation.action.jasper" extends="jasperreports-default">
  2.      <result-types>
  3.          <result-type name="jasper" class="org.apache.struts2.views.jasperreports.
  4. JasperReportsResult"></result-type>
  5.      </result-types>
  6.  
  7.      <action name="pullMaterielBill" class="com.olympus.sapg.smtinnovation.action.
  8. jasper.PrintPullMaterielBill">
  9.      <result name="success" type="jasper">
  10.           <param name="location">jasper\productionPlan\pullMaterielBill.jasper</param>
  11.           <param name="format">HTML</param>
  12.           <param name="dataSource">pullMaterialList</param>
  13.           <param name="reportParameters">Params</param>
  14.          </result>
  15.      </action>
  16. </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方法。

 
  1. private HashMap Params = new HashMap();
  2. public HashMap getParams() {
  3.      return Params; 
  4. }
  5.  
  6. public List<PullMaterial> getPullMaterialList(){
  7.      List<Criterion> criterionPullMaterial=new ArrayList<Criterion>();
  8.      List<Criterion> criterionPullMaterialBill=new ArrayList<Criterion>();
  9.      List<PullMaterial> pullMaterials = new ArrayList<PullMaterial>();
  10.  
  11.      criterionPullMaterialBill.add(Restrictions.between("billNumber",
  12.                  queryBillNumBegin,
  13.                  queryBillNumEnd));
  14.  
  15.      pullMaterials = pullMaterialService.pullMaterialBillMake(
  16.                      Order.asc("billNumber"),
  17.                      Order.asc("materialNumber"),
  18.                      criterionPullMaterial,
  19.                      criterionPullMaterialBill);
  20.  
  21.      Params.put("name""许亮");
  22.      return pullMaterials; 
  23. }  

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代码:

 
  1. <parameter name="reportParams.name" isforprompting="false" class="java.lang.String">
  2. </parameter> 

你可能感兴趣的:(apache,xml,Web,struts,Go)