问题
I have several similar reports in my application, for this reason I have created a basic structure with a subreport inside the Title and another one inside the Page Footer.
The problem is that I have 70 similar reports and if one day I need to change the footer structure I don´t wanna have to change 70 reports I prefer change only one.
In the footer subreport I have this:
<?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="reportFooterV" pageWidth="550" pageHeight="650" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="550" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">
<property name="ireport.zoom" value="1.5"/>
<property name="ireport.x" value="19"/>
<property name="ireport.y" value="0"/>
<parameter name="date" class="java.lang.String" isForPrompting="false"/>
<title>
<band height="19">
<textField>
<reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="0" y="0" width="191" height="19" forecolor="#999999"/>
<textElement verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
</textField>
<textField evaluationTime="Auto">
<reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
<textElement textAlignment="Right" verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$V{PAGE_NUMBER}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
But the variable PAGE_NUMBER, doesnt work and always show 1 in the page.
In the main report I have this in the pageFooter area
<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="systemParametersSummary" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" resourceBundle="properties.Messages" isIgnorePagination="true" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">
....
<pageFooter>
<band height="22">
<subreport>
<reportElement uuid="ac3cfc74-4e5a-45bc-945a-7ca6c82c4f6a" x="2" y="0" width="150" height="22"/>
<subreportParameter name="date">
<subreportParameterExpression><![CDATA[$P{date}]]></subreportParameterExpression>
</subreportParameter>
<subreportExpression><![CDATA[$P{footer}]]></subreportExpression>
</subreport>
</band>
</pageFooter>
</jasperReport>
I dont know why I cant find the workaround for this, if anyone can help me...thank you!
回答1:
If you're only using these sub-reports for the page-numbering then this is why you are getting an error. Each of these sub-reports is only 1 page long, so it will always be page 1 of 1.
Neither is there usually a need to use sub-reports for page numbering in this way, as the overhead for calling sub-reports is more than it's usually worth for any benefit you get from doing so.
I'd suggest that you get rid of your sub-reports, and just allow the main report to do the heavy-lifting e.g. page numbers across a full report with the use of Master report engine data by putting your text field for the date in the main report footer and also using this code for the page numbering:
<textField evaluationTime="Master">
<reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
<textElement textAlignment="Right" verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>
So, in your main report, you should end up with:
<pageFooter>
<band height="22">
<textField>
<reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="0" y="0" width="191" height="19" forecolor="#999999"/>
<textElement verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
</textField>
<textField evaluationTime="Master">
<reportElement uuid="89a04d3d-73e0-4f28-9747-3206c4022769" x="352" y="0" width="99" height="19" forecolor="#999999"/>
<textElement textAlignment="Right" verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>
</band>
</pageFooter>
Not forgetting that if you're using a sub-report in the main report Title band for a similar purpose then you should probably re-think the approach for that as well.
If there is a specific reason for this you can apply the same logic in a sub-report to be used as per the original question. As you are using Master page-numbering this will also give the correct results e.g. your sub-report code would look like this:
<?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="reportFooterV" pageWidth="550" pageHeight="650" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="550" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" uuid="6a63a92f-0859-4b0b-84b8-6166c2fe0951">
<parameter name="date" class="java.lang.String" isForPrompting="false"/>
<title>
<band height="22">
<textField>
<reportElement x="0" y="0" width="191" height="19" forecolor="#999999" uuid="89a04d3d-73e0-4f28-9747-3206c4022769"/>
<textElement verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$P{date}]]></textFieldExpression>
</textField>
<textField evaluationTime="Master">
<reportElement x="352" y="0" width="99" height="19" forecolor="#999999" uuid="89a04d3d-73e0-4f28-9747-3206c4022769"/>
<textElement textAlignment="Right" verticalAlignment="Bottom">
<font fontName="Roboto" size="10" isBold="false"/>
</textElement>
<textFieldExpression><![CDATA[$V{MASTER_CURRENT_PAGE} + " of " + $V{MASTER_TOTAL_PAGES}]]></textFieldExpression>
</textField>
</band>
</title>
</jasperReport>
来源:https://stackoverflow.com/questions/42064017/jasper-reports-page-number-inside-subreport-does-not-work