You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JasperReports嵌套对象列表渲染报错:未找到questions字段

解决JasperReports嵌套列表"Field not found : questions"错误

错误原因

错误核心是数据源上下文不匹配:questions是SectionDTO的属性,但当前的Questions列表组件位于主报表层级,主报表的数据源是ReportDTO,该类并不包含questions属性——必须将Questions列表嵌套在Section列表的<jr:listContents>内部,才能在当前Section的上下文里访问到对应的questions集合。

修改步骤

  1. 删除主报表层级独立的Questions列表组件
  2. 将Questions列表组件移入Section列表的<jr:listContents>标签内,作为Section内容的一部分
  3. 调整Section列表内容(<jr:listContents>)的高度,确保能容纳Section标题和嵌套的Questions列表

修改后的关键JRXML片段

<componentElement>
    <reportElement x="20" y="30" width="515" height="120" isRemoveLineWhenBlank="true" uuid="60c57e6e-924b-4576-8158-0670fe6847d2">
        <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
    </reportElement>
    <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
        <datasetRun subDataset="SectionDataSet" uuid="86e27911-12c2-436d-be4c-78996ab031d4">
            <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{sections})]]></dataSourceExpression>
        </datasetRun>
        <jr:listContents height="120" width="515">
            <!-- Section标题 -->
            <textField isBlankWhenNull="true">
                <reportElement x="1" y="0" width="514" height="20" uuid="bb1f7aeb-f199-4745-aab2-ffc877730573"/>
                <textElement>
                    <font size="12" isBold="true"/>
                    <paragraph lineSpacingSize="0.5"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
            </textField>
            <!-- 嵌套的Questions列表 -->
            <componentElement>
                <reportElement x="10" y="25" width="499" height="90" isRemoveLineWhenBlank="true" uuid="9a1f6d30-868d-4660-bdf7-3c58457e68aa">
                    <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/>
                </reportElement>
                <jr:list xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd" printOrder="Vertical">
                    <datasetRun subDataset="QuestionsDataSet" uuid="b40e2703-b00b-420a-944b-67b95a83445d">
                        <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{questions})]]></dataSourceExpression>
                    </datasetRun>
                    <jr:listContents height="42" width="499">
                        <textField>
                            <reportElement x="7" y="6" width="143" height="30" uuid="112822d7-7a6f-4c37-8fe8-387df4d0414d"/>
                            <textElement>
                                <font size="12" isBold="true"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{title}:]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="150" y="6" width="349" height="30" uuid="3793156d-7daf-4632-a77d-70f303b97418"/>
                            <textFieldExpression><![CDATA[$F{text}]]></textFieldExpression>
                        </textField>
                    </jr:listContents>
                </jr:list>
            </componentElement>
        </jr:listContents>
    </jr:list>
</componentElement>

修改后,报表会先遍历每个Section,再在每个Section内部遍历对应的Questions列表,数据源上下文匹配后即可正确访问questions字段。

内容的提问来源于stack exchange,提问作者diminuta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 13:15:38