此篇文章翻译与Apache官方网站的JasperReports API。JasperReports在线API地址为:http://struts.apache.org/2.1.8.1/struts2-plugins/struts2-jasperreports-plugin/apidocs/org/apache/struts2/views/jasperreports/JasperReportsResult.html。
org.apache.struts2.views.jasperreports
Class JasperReportsResult

所有的应用接口
com.opensymphony.xwork2.Result, java.io.Serializable, org.apache.struts2.StrutsStatics,JasperReportConstants
公共类方法
- public class JasperReports extends org.apache.struts2.dispatcher.StrutsResultSupport
- implements JasperReportConstants {
- //你的代码
- }
要生成一个JasperReports报表,必须要为其指定一种特殊的格式;若没有指定,则默认生成PDF格式的报表。
JasperReports报表的result type参数如下:
- location (默认):定义已编译的jasper report 文件的路径,如(foo.jasper),一般情况下是相对的URL路径,如 /report/my/test.jasper
- dataSource(必须的参数):数据源,通常是一个List。
- parse:默认值为true。如果设置成false,将不会解析location所携带的参数。
- format:报表生成的格式。可用的报表格式有CSV、HTML、PDF、RTF、XLS和XML;若此参数值为空或省略了,生成的报表格式默认为PDF文档。
- contentDisposition:内容形式(默认值为“inline”,其值为典型的文件名="document.pdf")。
- documentName:文档名称。
- delimiter:定界符,当生成的报表为CSV格式时,该参数才有效,默认的定界符为英文状态下的逗号“,”。
- imageServletUrl:URL的名字,至于页面上下文之前,会返回一张报表图片。
- reportParameters:报表参数。(struts 2.1.2+以上版本才支持)传递给报表的参数,通过map(key,value)方法实现。
- exportParameters:报表导出参数。(struts 2.1.2+以上版本才支持)例如,生成一个PDF报表,可以为PDF报表设置一个String类型的密码,此时就会用的exportParameters参数。
- connection:连接信息。(struts 2.1.7+以上版本才支持)可以不使用dataSource,取而代之的为JDBC连接信息,来作为报表的数据源。
使用实例(传统方法)
要生成报表,则在struts.xml文件中配置
- <!-- CSV格式报表 -->
- <result name="success" type="jasper">
- <param name="location">foo.jasper</param>
- <param name="dataSource">mySource</param>
- <param name="format">CSV</param>
- </result>
- <!-- PDF格式报表 -->
- <result name="success" type="jasper">
- <param name="location">foo.jasper</param>
- <param name="dataSource">mySource</param>
- </result>
零配置方式
此中方法无需在struts.xml文件中配置,直接继承struts2的ActionSupport接口,关于struts2的零配置,请在网上自行搜索资料,这里就不详细讲解,稍后有空我再写一遍关于struts2零配置的文章。
- package com.olympus.sapg.smtinnovation.action.jasper;
- import com.opensymphony.xwork2.ActionSupport;
- public class PullMaterielPickUpRecordJasper
- extends ActionSupport{
- @Action(value="MaterielPickUpRecordExport",
- results={@Result(
- name="success",
- location="/jasper/sapdocument/sapdoc_MaterielPickUpRecord.jasper",
- type="jasper",
- params={
- "dataSource","pullMaterielRecords",
- "format","XLS",
- "documentName","Pull_Materiel_Records"})})
- public String MaterielPickUpRecordExport(){
- return SUCCESS;
- }
- }