您的位置:首页 > 编程语言 > ASP

struts2+JasperReports -jasperReports学习笔记 -gcode

2014-05-13 14:36 232 查看
安装JasperReports插件

其中奇怪的是JasperReports插件中的struts-plugin.xml中居然没有继承struts-default包。

修改,使继承struts-default包。

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC

    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

    "http://struts.apache.org/dtds/struts-2.0.dtd">

    

<struts>

    <package name="jasperreports-default" extends="struts-default">

    

    <result-types>

       <result-type name="jasper" class="org.apache.struts2.views.jasperreports.JasperReportsResult"/>

    </result-types>

    </package> </struts>

======================================

一,JasperAction.java--

package lee; import com.opensymphony.xwork2.ActionSupport;

import java.util.*; public class JasperAction extends ActionSupport

{

public List getBookList()

{

   List result = new ArrayList();

   result.add(new Book(1 , "Spring2.0宝典" , "李刚"));

   result.add(new Book(2 , "轻量级J2EE企业应用实战" , "李刚"));

   result.add(new Book(3 , "基于J2EE的Ajax宝典" , "李刚"));

   return result;

}

}

--------------

二,配置JasperAction--

<?xml version="1.0" encoding="GBK"?>

<!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.custom.i18n.resources" value="messageResource"/>

<constant name="struts.i18n.encoding" value="GBK"/>

<package name="lee" extends="jasperreports-default"> 

   <action name="export" class="lee.JasperAction">

    <result name="success" type="jasper">

     <param name="location">jasper\sample_report.jasper</param>

     <param name="format">HTML</param>

     <param name="dataSource">bookList</param>

    </result>

   </action>

</package> </struts>

----

1,Result为jasper类型时需要配置三个属性。

location指定生成报表所用的.jasper文件位置。在应用根目录的jasper文件夹下。

format指定生成的报表格式,默认是PDF。

dataSource指定一个集合属性名,JasperReports将自动迭代输出该集合中的VO对象。

2,需注意,这里也要用字段来输出action中的集合元素。

3,在浏览器输入export.action即可看到生成的报表。

如果在定义export action时,指定success逻辑视图对应的报表格式为PDF,再次发送export.action请求时,

会得到PDF文档。系统会下载一份文件,后缀是action,改为pdf后缀后可看出是pdf文档。

如果希望系统自动使用pdf后缀,可以在struts2拦截请求的后缀中增加pdf,

然后发送export.pdf请求即可。

---------------

三,sample_report.jrxml--

<?xml version="1.0" encoding="UTF-8" ?>

<!-- Created with iReport - A designer for JasperReports -->

<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">

<jasperReport

   name="sample_report"

   columnCount="1"

   printOrder="Vertical"

   orientation="Portrait"

   pageWidth="595"

   pageHeight="842"

   columnWidth="535"

   columnSpacing="0"

   leftMargin="30"

   rightMargin="30"

   topMargin="20"

   bottomMargin="20"

   whenNoDataType="NoPages"

   isTitleNewPage="false"

   isSummaryNewPage="false">

<property name="ireport.scriptlethandling" value="0" />

<property name="ireport.encoding" value="UTF-8" />

<import value="java.util.*" />

<import value="net.sf.jasperreports.engine.*" />

<import value="net.sf.jasperreports.engine.data.*" /> <style 

   name="zh"

   isDefault="false"

   fontName="宋体"

   fontSize="18"

   isBold="true"

   pdfFontName="STSong-Light"

   pdfEncoding="UniGB-UCS2-H"

/> 

<field name="id" class="java.lang.Integer"/>

<field name="name" class="java.lang.String"/>

<field name="author" class="java.lang.String"/>    <background>

    <band height="0" isSplitAllowed="true" >

    </band>

   </background>

   <title>

    <band height="50" isSplitAllowed="true" >

    </band>

   </title>

   <pageHeader>

    <band height="50" isSplitAllowed="true" >

    </band>

   </pageHeader>

   <columnHeader>

    <band height="39" isSplitAllowed="true" >

     <staticText>

      <reportElement

       style="zh"

       x="93"

       y="0"

       width="62"

       height="31"

       key="staticText-2"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <text><![CDATA[图书ID]]></text>

     </staticText>

     <staticText>

      <reportElement

       style="zh"

       x="170"

       y="0"

       width="83"

       height="31"

       key="staticText-3"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <text><![CDATA[图书作者]]></text>

     </staticText>

     <staticText>

      <reportElement

       style="zh"

       x="261"

       y="0"

       width="191"

       height="31"

       key="staticText-4"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <text><![CDATA[图书书名]]></text>

     </staticText>

    </band>

   </columnHeader>

   <detail>

    <band height="45" isSplitAllowed="true" >

     <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

      <reportElement

       style="zh"

       x="93"

       y="12"

       width="62"

       height="33"

       key="textField"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <textFieldExpression   class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>

     </textField>

     <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

      <reportElement

       style="zh"

       x="261"

       y="12"

       width="231"

       height="33"

       key="textField"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <textFieldExpression   class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>

     </textField>

     <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

      <reportElement

       style="zh"

       x="170"

       y="12"

       width="83"

       height="33"

       key="textField"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <textFieldExpression   class="java.lang.String"><![CDATA[$F{author}]]></textFieldExpression>

     </textField>

    </band>

   </detail>

   <columnFooter>

    <band height="30" isSplitAllowed="true" >

     <staticText>

      <reportElement

       style="zh"

       x="226"

       y="5"

       width="103"

       height="23"

       key="staticText-5"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <text><![CDATA[当前页码:]]></text>

     </staticText>

     <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None" hyperlinkTarget="Self" >

      <reportElement

       style="zh"

       x="339"

       y="5"

       width="140"

       height="23"

       key="textField"/>

      <box topBorder="None" topBorderColor="#000000" leftBorder="None" leftBorderColor="#000000" rightBorder="None" rightBorderColor="#000000" bottomBorder="None" bottomBorderColor="#000000"/>

      <textElement>

       <font/>

      </textElement>

     <textFieldExpression   class="java.lang.Integer"><![CDATA[$V{PAGE_COUNT}]]></textFieldExpression>

     </textField>

    </band>

   </columnFooter>

   <pageFooter>

    <band height="50" isSplitAllowed="true" >

    </band>

   </pageFooter>

   <lastPageFooter>

    <band height="50" isSplitAllowed="true" >

    </band>

   </lastPageFooter>

   <summary>

    <band height="50" isSplitAllowed="true" >

    </band>

   </summary>

</jasperReport>

---------------------------

四,Book.java--

package lee; public class Book

{

private int id;

private String name;

private String author; public Book()

{

} public Book(int id , String name , String author)

{

   this.id = id;

   this.name = name;

   this.author = author;

} public void setId(int id)

{

   this.id = id;

}

public int getId()

{

   return this.id;

} public void setName(String name)

{

   this.name = name;

}

public String getName()

{

   return this.name;

} public void setAuthor(String author)

{

   this.author = author;

}

public String getAuthor()

{

   return this.author;

} }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: