Struts2整合Jasperreport

首先做一个struts2项目框架

 

iReport设计一个报表文件(.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="Strudents"

        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="2" />

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

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

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

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

    <reportFont name="china" isDefault="false" fontName="瀹嬩綋" size="14" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfFontName="STSong-Light" pdfEncoding="UniGB-UCS2-H" isPdfEmbedded="false"/>

 

 

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

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

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

 

       <background>

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

           </band>

       </background>

       <title>

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

              <staticText>

                  <reportElement

                     x="175"

                     y="3"

                     width="182"

                     height="28"

                     key="staticText-1"/>

                  <box></box>

                  <textElement textAlignment="Center">

                     <font fontName="瀹嬩綋" pdfFontName="STSong-Light" size="18" isBold="true" pdfEncoding ="UniGB-UCS2-H"/>

                  </textElement>

              <text><![CDATA[瀛︾敓鑺卞悕鍐宂]></text>

              </staticText>

           </band>

       </title>

       <pageHeader>

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

              <staticText>

                  <reportElement

                     x="62"

                     y="3"

                     width="99"

                     height="22"

                     key="staticText-2"/>

                  <box></box>

                  <textElement textAlignment="Center">

                     <font reportFont="china"/>

                  </textElement>

              <text><![CDATA[瀛﹀彿锛歖]></text>

              </staticText>

              <staticText>

                  <reportElement

                     x="373"

                     y="3"

                     width="99"

                     height="22"

                     key="staticText-3"/>

                  <box></box>

                  <textElement textAlignment="Center">

                     <font reportFont="china"/>

                  </textElement>

              <text><![CDATA[鐢佃瘽锛歖]></text>

              </staticText>

              <staticText>

                  <reportElement

                     x="212"

                     y="3"

                     width="99"

                      height="22"

                     key="staticText-4"/>

                  <box></box>

                  <textElement textAlignment="Center">

                     <font reportFont="china"/>

                  </textElement>

              <text><![CDATA[濮撳悕锛歖]></text>

              </staticText>

           </band>

       </pageHeader>

       <columnHeader>

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

           </band>

       </columnHeader>

       <detail>

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

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

                  <reportElement

                     x="62"

                     y="9"

                     width="99"

                     height="18"

                     key="textField"/>

                  <box></box>

                  <textElement>

                     <font reportFont="china"/>

                  </textElement>

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

              </textField>

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

                  <reportElement

                     x="212"

                     y="9"

                     width="100"

                     height="18"

                     key="textField"/>

                  <box></box>

                  <textElement>

                     <font reportFont="china"/>

                  </textElement>

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

              </textField>

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

                  <reportElement

                     x="373"

                     y="9"

                     width="100"

                     height="18"

                     key="textField"/>

                  <box></box>

                  <textElement>

                     <font reportFont="china"/>

                  </textElement>

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

              </textField>

           </band>

       </detail>

       <columnFooter>

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

           </band>

       </columnFooter>

       <pageFooter>

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

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

                  <reportElement

                     x="333"

                     y="5"

                     width="100"

                     height="18"

                     key="textField"/>

                  <box></box>

                  <textElement textAlignment="Right">

                     <font/>

                  </textElement>

              <textFieldExpression   class="java.lang.String"><![CDATA["Page " + $V{PAGE_NUMBER} + " of "]]></textFieldExpression>

              </textField>

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

                  <reportElement

                     x="433"

                     y="5"

                     width="100"

                     height="18"

                     key="textField"/>

                  <box></box>

                  <textElement>

                     <font/>

                  </textElement>

              <textFieldExpression   class="java.lang.String"><![CDATA["" + $V{PAGE_NUMBER} + ""]]></textFieldExpression>

              </textField>

           </band>

       </pageFooter>

       <lastPageFooter>

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

           </band>

       </lastPageFooter>

       <summary>

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

           </band>

       </summary>

</jasperReport>

Strudents.jrxml放在web应用的WebRoot\Jasper\Strudents.jrxml

 

往项目里加入支持Jasperreportjar文件
struts2-jasperreport-plugin-2.0.12.jar
jasperreports-2.0.5.jar
commons-digester-1.7.jar
commons-beanutile-1.7.jar
commons-collections-2.1.jar
itext-1.3.1.jar
iTextAsian.jar
poi-3.0-alpha3-20061212.jar

 

写一个提供数据源的javaBean:
package bean;

 

public class Student {

    private int stuid;

    private String stuname;

    private String stuphoto;

    public Student(int stuid,String stuname,String stuphoto){

       this.stuid = stuid;

       this.stuname = stuname;

       this.stuphoto = stuphoto;

    }

    public int getStuid() {

       return stuid;

    }

    public void setStuid(int stuid) {

       this.stuid = stuid;

    }

    public String getStuname() {

       return stuname;

    }

    public void setStuname(String stuname) {

       this.stuname = stuname;

    }

    public String getStuphoto() {

       return stuphoto;

    }

    public void setStuphoto(String stuphoto) {

       this.stuphoto = stuphoto;

    }

 

}

写一个action:
package action;

 

import java.io.File;

import java.util.ArrayList;

import java.util.List;

 

import net.sf.jasperreports.engine.JasperCompileManager;

 

import org.apache.struts2.ServletActionContext;

 

import bean.Student;

 

import com.opensymphony.xwork2.ActionSupport;

 

public class StudentJasperAction extends ActionSupport {

 

    private List student;

 

    public List getStudent() {

       student = new ArrayList();

       for(int i=0;i<50;i++){

           Student stu = new Student(i,"张三"+i,"1503669100"+i);

           student.add(stu);

       }

      

       return student;

    }

 

    public String execute() throws Exception {

       // TODO Auto-generated method stub

       String sourceFile = ServletActionContext.getServletContext()

              .getRealPath("/Jasper/Strudents.jrxml");

       File parent = new File(sourceFile).getParentFile();

       JasperCompileManager.compileReportToFile(sourceFile, new File(parent,"/Strudents.jasper").getAbsolutePath());

       return super.execute();

    }

 

}

 

修改struts.xml:
<?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="struts2" extends="jasperreports-default">

       <action name="PDF" class="action.StudentJasperAction">

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

              <param name="location">Jasper/Strudents.jasper</param>

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

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

           </result>

       </action>

       <action name="HTML" class="action.StudentJasperAction">

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

              <param name="location">Jasper/Strudents.jasper</param>

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

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

           </result>

       </action>

       <action name="XML" class="action.StudentJasperAction">

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

              <param name="location">Jasper/Strudents.jasper</param>

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

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

           </result>

       </action>

       <action name="XLS" class="action.StudentJasperAction">

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

              <param name="location">Jasper/Strudents.jasper</param>

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

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

           </result>

       </action>

    </package>

</struts>

 

写一个jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>

    <base href="<%=basePath%>">

   

    <title>My JSP 'index.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

  </head>

 

  <body>

    打印、预缆报表 <hr>

    <a href="PDF.action">PDF格式报表</a><br/>

    <a href="HTML.action">HTML格式报表</a><br/>

    <a href="XML.action">XML格式报表</a><br/>

    <a href="XLS.action">EXCLE格式报表</a>

  </body>

</html>

 

为了支持HTML显示时图片加载不上的问题,我们放一个图片文件进去,地址是WebRoot\images\px 命名一定要和我的一样。

近期我将会把Struts2整合Jasperreport录制成视频,敬请关注

posted on 2009-06-11 15:01  JavaVIQ  阅读(3280)  评论(0编辑  收藏  举报