':' JasperReports HTML 输出中的字符
我正在使用 JasperReports,以 HTML 作为导出格式。问题是,在我的 Java 类中,我使用包含“:
”字符的字符串值设置参数,当我单击按钮生成报告时,包含“”的字段:
' 字符,更改其位置并显示在其他地方,如果我删除 ':
',它工作正常。
一些输入
- 我的 jrxml 是 UTF-8 编码的
- 我尝试用 '
\u003a
' 替换 ':
' 仍然没有运气 - 我尝试替换 '
:
'与 ':
' 这给了我 ':
' 但不是 ':
' - jrxml 是东西像这样,在这里'leaveTime' 的值带有字符 ':',因此它不会显示在表中,而是显示在屏幕上的其他位置(具有不同的 x 和 y 值)。
<detail>
<band height="15">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="600" y="0" width="120" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{supervisorName}]]>
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="532" y="0" width="65" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{leaveTime}]]>
</textFieldExpression>
</textField>
</band>
</detail>
I am working with JasperReports with HTML as the export format. The problem is that from my Java class, I set the parameter with a String value containing the ':
' character, when I click the button to produce the report, the field which contains the ':
' character, change its position and showed somewhere else, if I remove the ':
', it works fine.
Some inputs
- My jrxml is UTF-8 encoded
- I tried replacing '
:
' with '\u003a
' still no luck - I tried replacing '
:
' with ':
' this gives me ':
' but not ':
' - The jrxml is something like this, here the 'leaveTime' has the value with a character ':' and because of that it does not display in the table but displayed somewhere else on the screen (with different x and y value).
<detail>
<band height="15">
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="600" y="0" width="120" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{supervisorName}]]>
</textFieldExpression>
</textField>
<textField isStretchWithOverflow="true" isBlankWhenNull="true">
<reportElement x="532" y="0" width="65" height="15"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[$F{leaveTime}]]>
</textFieldExpression>
</textField>
</band>
</detail>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本思想是这样的:JasperReports 将 $F{leaveTime} 的任何值放入报告中。这可能包括 HTML 不友好的字符。这是合理的,因为 JR 不知道您是否会导出为 Excel、PDF、HTML 或其他格式。您将 '
:
' 替换为 '\u003a
' 的想法在概念上是正确的......但在细节上不太正确。您需要 HTML 转义,而不是 UTF-8 转义。我认为在这种特定情况下您需要
:
。更一般地,您应该使用为转义 HTML 字符串而构建的库。我建议使用 Apache Commons Lang。这样您就可以将
$F{leaveTime}
替换为StringEscapeUtils.escapeHtml($F{leaveTime})
。The basic idea is this: JasperReports is putting whatever value you have for $F{leaveTime} into the report. This could include characters that are not HTML-friendly. This is reasonable, since JR doesn't know if you will export to Excel or PDF or HTML or something else. Your idea of replacing '
:
' with '\u003a
' is correct conceptually... but not quite right in its details. You need HTML escaping, not UTF-8 escaping.I think in this specific case you need
:
.More generally you should use a library built for escaping HTML strings. I recommend using Apache Commons Lang. That way you could replace
$F{leaveTime}
withStringEscapeUtils.escapeHtml($F{leaveTime})
.