Jsp将参数传递给jrxml中的subdatset

时间:2016-01-14 03:46:10

标签: java jsp jasper-reports

我想将一个参数从jsp传递给jrxml,该参数在subdataset中的查询中使用,但是当我尝试时,我总是得到空的pdf。

如果我尝试没有参数的sql我没有问题。所以我猜测我传递参数的方式有问题。

show.jsp

<%
    Connection conn = null;
    //int id = Integer.parseInt(request.getParameter("ID"));
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","passwd1234");

        File reportFile = new File(application.getRealPath("//T0113.jasper"));
        System.out.println(reportFile);
        Map parameters = new HashMap();
        String sqlCondition = "A11";
        parameters.put("id",sqlCondition);
        byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);
        System.out.println(parameters);
        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        ServletOutputStream outStream = response.getOutputStream();
        outStream.write(bytes,0,bytes.length);
        outStream.flush();
        outStream.close();  
    } catch (Exception ex) {
        out.println("Error : " + ex.getMessage());
    }


%>

我的jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.2.0.final using JasperReports Library version 6.2.0  -->
<!-- 2016-01-14T16:09:41 -->
<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="T0113" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="84489b82-cfa1-489a-a5e2-404ca58708d2">
    <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TEST0114"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="T0113" uuid="e911ce63-0ce5-4bb2-b3c8-e6b41cba5852">
        <property name="com.jaspersoft.studio.data.sql.tables" value=""/>
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="TEST0114"/>
        <parameter name="id" class="java.lang.String"/>
        <queryString>
            <![CDATA[select * from person where '$P{id}']]>
        </queryString>
        <field name="ID" class="java.lang.String">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <field name="NAME" class="java.lang.Integer">
            <fieldDescription><![CDATA[]]></fieldDescription>
        </field>
        <group name="ID">
            <groupExpression><![CDATA[$F{ID}]]></groupExpression>
        </group>
        <group name="NAME">
            <groupExpression><![CDATA[$F{NAME}]]></groupExpression>
        </group>
    </subDataset>
    <queryString>
        <![CDATA[select * from person]]>
    </queryString>
    <field name="ID" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="NAME" class="java.lang.Integer">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <group name="ID">
        <groupExpression><![CDATA[$F{ID}]]></groupExpression>
    </group>
    <group name="NAME">
        <groupExpression><![CDATA[$F{NAME}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <pageHeader>
        <band height="215" splitType="Stretch">
            <componentElement>
                <reportElement x="170" y="15" width="200" height="200" uuid="02e57f61-e30e-429f-a8c6-487a18e7ed07">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="T0113" uuid="dd2d7275-311a-4bd0-b7d8-d9948ddc3c1c">
                        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    </datasetRun>
                    <jr:column width="100" uuid="a3a9e148-5c8d-44c9-a76d-38a1b86daf01">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="8eba1ee4-42c7-4908-a97b-88b6a53faa1e"/>
                                <text><![CDATA[ID]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="5ee52917-3b1b-4ba3-aed8-581662fb8cd9"/>
                                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                    <jr:column width="100" uuid="7a689d13-3e08-4683-a53b-768c15825885">
                        <jr:columnHeader style="Table_CH" height="30">
                            <staticText>
                                <reportElement x="0" y="0" width="100" height="30" uuid="d821fdb7-948a-4f67-90d8-3bf9f0f6da70"/>
                                <text><![CDATA[NAME]]></text>
                            </staticText>
                        </jr:columnHeader>
                        <jr:detailCell style="Table_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="100" height="30" uuid="0f29f475-aff1-4bad-855a-145e393717cd"/>
                                <textFieldExpression><![CDATA[$F{NAME}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </pageHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

错误消息

parameter index out of range (1 number of parameters which is 0)

1 个答案:

答案 0 :(得分:0)

您正在将参数正确jsp传递到jrxml,这不是您的问题。

问题您没有将参数从主报告传递到subDataset,实际上该参数在主报告中不存在。

<强>解决方案:

  1. 同样在主报告中定义参数,名称相同。

    <parameter name="id" class="java.lang.String"/>
    

    此定义位于subDataset

  2. datasetRun表达式中的主报告参数传递给subDataset

    <datasetRun subDataset="T0113" uuid="dd2d7275-311a-4bd0-b7d8-d9948ddc3c1c">
        <datasetParameter name="id">
            <datasetParameterExpression><![CDATA[$P{id}]]></datasetParameterExpression>
        </datasetParameter>
        <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    </datasetRun>
    
  3. 要理解这一点,您需要将subDataset视为子报告,作为另一个报告。主报表中的参数不会自动出现在需要定义的subDataset中,并且需要传递它们的值。

    如果您暂时不传递所有参数,也可以使用此表达式将整个参数地图传递给subdataset。 (注意,您仍然需要在主报告和subdataset)中定义它们

    <datasetRun subDataset="T0113" uuid="dd2d7275-311a-4bd0-b7d8-d9948ddc3c1c">
            <parametersMapExpression><![CDATA[new java.util.HashMap($P{REPORT_PARAMETERS_MAP})]]></parametersMapExpression>
            <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
    </datasetRun>
    

    问题范围之外的一些评论

    1. 您的查询select * from person where '$P{id}'似乎有拼写错误,可能应该是select * from person where theCorrectColumnNameThatIsAVarChar = $P{id}
    2. 我看到细节带是空的,没有必要执行 如果要显示,请在主报告中查询select * from person 使用空数据源报告只需使用该属性 whenNoDataType="AllSectionsNoDetail"代码
    3. 上的jasperReport