使用Struts2框架的Jasper报告

时间:2013-02-13 07:23:57

标签: jsp struts2 jasper-reports

我已在Struts2框架中创建了一个带有Jasper报告的应用程序。

在单击按钮的索引页面中,我将以PDF格式显示报告。

但是我非常关心我的实施。实现我所做的是正确的方法吗?

因为我在几个教程中注意到在struts2中调用jasper有点像:

  <action name="myJasperTest" class="com.acme.test.action.JasperAction">
        <result name="success" type="jasper">
            <param name="location">/jasper/our_compiled_template.jasper</param>
            <param name="dataSource">myList</param>
            <param name="format">PDF</param>
        </result>
  </action>

但我不知道如何通过上述方式将参数传递给查询? 任何人都可以告诉我一些解决方案或建议

的index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
       <s:form action="getUserDetails"  method="post">
            <s:submit value="Show" onclick="this.form.target='_blank';return true;"/>           
        </s:form>
    </body>
</html>

JasperReport.java

package commonpackage;

import com.opensymphony.xwork2.ActionSupport;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.struts2.ServletActionContext;

public class JasperReport extends ActionSupport
{
     private String userid="1";
     public InputStream inputStream;
      public String getUserDetails() throws ClassNotFoundException{

            try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/ortms", "root", "");
            String str;
            str = ServletActionContext.getServletContext().getRealPath("/reports/myreport.jrxml");
            JasperDesign jd=JRXmlLoader.load(str);
            Map parameters = new HashMap();
            parameters.put("userid", userid);
            net.sf.jasperreports.engine.JasperReport jr = JasperCompileManager.compileReport(jd);
            JasperPrint jp=JasperFillManager.fillReport(jr, parameters,cn);
            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, ServletActionContext.getServletContext().getRealPath("/reports/myreport.pdf"));
            exporter.exportReport();
            File file = new File(ServletActionContext.getServletContext().getRealPath("/reports/myreport.pdf"));  
            inputStream = new DataInputStream( new FileInputStream(file));

            return SUCCESS;

        } catch (Exception e) {
            System.out.println("Exception in getUserDetails in JasperReport class");
            e.printStackTrace();
        }

   return SUCCESS;
}

      public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }



public InputStream getInputStream() {
        return inputStream;
    }
}

struts.xml中

<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
    <include file="example.xml"/>
    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
      <action name="getUserDetails" class="commonpackage.JasperReport" method="getUserDetails">
         <result name="success" type="stream">
           <param name="contentType">application/pdf</param>
           <param name="inputName">inputStream</param>
           <param name="contentDisposition">inline;filename="UserReport.pdf"</param>
           <param name="bufferSize">1024</param>
         </result>
      </action>
    </package>
</struts>

myreport.jrmxl

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="70b2040b-a566-48b5-8e8e-a01d402007e3">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <parameter name="userid" class="java.lang.String">
        <defaultValueExpression><![CDATA[0]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT
     table_users.`id` AS table_users_id,
     table_users.`user_code` AS table_users_user_code,
     table_users.`first_name` AS table_users_first_name,
     table_users.`last_name` AS table_users_last_name
FROM
     `table_users` table_users WHERE table_users.`id`=$P{userid}]]>
    </queryString>
    <field name="table_users_id" class="java.lang.Integer"/>
    <field name="table_users_user_code" class="java.lang.String"/>
    <field name="table_users_first_name" class="java.lang.String"/>
    <field name="table_users_last_name" class="java.lang.String"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="20" splitType="Stretch">
            <staticText>
                <reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="33" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[ID]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="132" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[CODE]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="232" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[FIRST NAME]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="ca3f2664-22a6-4f31-b04b-f61b774d2dc3" x="332" y="0" width="100" height="20"/>
                <textElement>
                    <font isBold="true"/>
                </textElement>
                <text><![CDATA[LAST NAME]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="33" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{table_users_id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="133" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{table_users_user_code}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="232" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{table_users_first_name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="095242c4-b1b5-4545-897c-315f3bbe37a2" x="332" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{table_users_last_name}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

1 个答案:

答案 0 :(得分:1)

关于文档,你应该这样做:

reportParameters - (2.1.2+) OGNL expression used to retrieve a map of report 
parameters from the value stack. The parameters may be accessed in the report 
via the usual JR mechanism and might include data not part of the dataSource, 
such as the user name of the report creator, etc.

所以你应该有这个配置:

<action name="myJasperTest" class="com.acme.test.action.JasperAction">
        <result name="success" type="jasper">
            <param name="location">/jasper/our_compiled_template.jasper</param>
            <param name="dataSource">myList</param>
            <param name="format">PDF</param>
            <param name="exportParameters">parameterMap</param>
        </result>
  </action>

以下是Document