我用的是struts2.1.6,从struts2的自带的demo当中可以看到它的web.xml配置与之前的有点不同,有另外一种配置:
<filter> <filter-name>Struts2</filter-name> <filter-class> org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter </filter-class> </filter> <filter-mapping> <filter-name>Struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
这样的配置可以在web.xml配置我们的serlvet,如果写成以前的写法你配置上serlvet会报错。
将我们设置的报表文件和编译文件放到WebRoot下面的jasper下面,添加struts2对jasperReport的插件。
构造我们的JavaBean如下(get,set方法省略了):
public class Person { private String person_Id; private String person_name; private String person_age; private String person_address; }
构造我们的service:
public class PersonService { public List<Person> getAllPerson() { List<Person> perList = new ArrayList<Person>(); perList.add(new Person("101", "小博", "22", "湖北")); perList.add(new Person("102", "张三", "21", "湖南")); perList.add(new Person("103", "李四", "23", "江苏")); perList.add(new Person("104", "王五", "22", "上海")); return perList; } }
构造action:
public class PersonAction extends ActionSupport { private List<Person> presonList = null; private Map<String, String> reportParameter = null; @Override public String execute() throws Exception { presonList = new PersonService().getAllPerson(); reportParameter = new HashMap<String, String>(); reportParameter.put("year", "2009"); reportParameter.put("unit_mc", "武汉XX科技有限公司"); return SUCCESS; } public String htmlView(){ return SUCCESS; } public List<Person> getPresonList() { return presonList; } public void setPresonList(List<Person> presonList) { this.presonList = presonList; } public Map<String, String> getReportParameter() { return reportParameter; } public void setReportParameter(Map<String, String> reportParameter) { this.reportParameter = reportParameter; } }
struts配置如下:
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.action.extension" value="action" /> <package name="jasperReport" extends="struts-default,jasperreports-default"> <action name="PDF" class="com.mengya.action.PersonAction"> <result name="success" type="jasper"> <param name="location">/jasper/preson.jasper</param> <param name="dataSource">presonList</param> <param name="reportParameters">reportParameter</param> <param name="format">PDF</param> </result> </action> <action name="XLS" class="com.mengya.action.PersonAction"> <result name="success" type="jasper"> <param name="location">/jasper/preson.jasper</param> <param name="dataSource">presonList</param> <param name="reportParameters">reportParameter</param> <param name="format">XLS</param> </result> </action> <action name="XML" class="com.mengya.action.PersonAction"> <result name="success" type="jasper"> <param name="location">/jasper/preson.jasper</param> <param name="dataSource">presonList</param> <param name="reportParameters">reportParameter</param> <param name="format">XML</param> </result> </action> <action name="CSV" class="com.mengya.action.PersonAction"> <result name="success" type="jasper"> <param name="location">/jasper/preson.jasper</param> <param name="dataSource">presonList</param> <param name="reportParameters">reportParameter</param> <param name="format">CSV</param> </result> </action> <!-- location:是指我们刚才用iReport编译生成的jasper文件 dataSource:是指我们执行的数据库查询结果,在testAction类里把这个结果查询出来,这个参数必须要有 format:是指需要输出的类型,默认是PDF,其他的输出类型有:XML、HTML、XLS、CSV、RTF ,注意这里一定要大写 reportParameters:jasperreport中的Parameters 除了这几个参数,还有下列参数: delimiter:是指如果输出类型为CSV的话,指定分割符,默认为“,” parse:是指是否解析location参数中的EL表达式,默认为 true contentDisposition:指定disposition,默认为“inline”,如果设为“attachment”就是强制下载 documentName:输出的文件名 imageServletUrl:生成图形的路径 --> </package> </struts>
页面调用:
<a href="PDF.action">PDF</a> <a href="XLS.action">XLS</a> <a href="XML.action">XML</a> <a href="CSV.action">CSV</a>
如果要想以HTML形式显示报表内容则还需要在web.xml配置jasperReport自带的一个servlet如下:
<!-- JasperReport包中自带的Servlet,用来做为HTML形式显示报表的时候报表的图片,要在这里配置上,图片在jasperReport包中 --> <servlet> <servlet-name>JasperReportImageServlet</servlet-name> <servlet-class> net.sf.jasperreports.j2ee.servlets.ImageServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>JasperReportImageServlet</servlet-name> <url-pattern>/image</url-pattern> </servlet-mapping>
action中不需要修改,只需在struts.xml里添加一个HTML不配置如下:
<action name="HTML" class="com.mengya.action.PersonAction"> <result name="success" type="jasper"> <param name="location">/jasper/preson.jasper</param> <param name="dataSource">presonList</param> <param name="reportParameters">reportParameter</param> <param name="format">HTML</param> <param name="imageServletUrl"> <![CDATA[/image?image=]]> </param> </result> </action>
imageServletUrl的值就是上面的serlvet的路径,该servlet就是生成了一个图形,如果不配置该serlvet则页面上有图片没显示的样子。
页面调用如:
<a href="HTML.action">HTML</a>