如何在 JasperReports 的报告中添加 JavaScript 行?
我添加了一个静态文本字段并将其标记为HTML并将文本插入为<script></script>
但是在导出
时它不会显示 JavaScript<band height="79" splitType="Stretch">
<staticText>
<reportElement mode="Opaque" x="0" y="0" width="555" height="79" backcolor="#EEEEEE"/>
<textElement markup="html"/>
<text><![CDATA[<script type="text/javascript">window.alert('hello');</script>]]></text>
</staticText>
</band>
我是否缺少更多要设置的属性?我无法找到解释如何在报告中嵌入 JavaScript 代码的具体文档。如果可以,请提供任何文件的链接
答案 0 :(得分:1)
我不认为您在HTML标记中添加javascript的方式将在导出的HTML中呈现为脚本。但是我知道另一种方式。请参阅下面的代码段:
StringBuffer sbuf = new StringBuffer();
net.sf.jasperreports.engine.export.JRHtmlExporter exporter = new net.sf.jasperreports.engine.export.JRHtmlExporter();
exporter.setParameter(net.sf.jasperreports.engine.JRExporterParameter.OUTPUT_STRING_BUFFER, sbuf);
.
.
.
exporter.exportReport();
sbuf.append("<script type=\"text/javascript\">window.alert('hello');</script>");
String content = sbuf.toString();
参数OUTPUT_STRING_BUFFER将导出的输出发送到StringBuffer(在本例中为 sbuf )。然后,您可以将脚本附加到此导出的HTML,最后使用JSP打印内容,如下所示:
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
<meta http-equiv='Content-Disposition' content='inline'>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" style="background-color:white">
<tr>
<td width="100%" valign="middle" align="center">
<%=content%>
</td>
</body>
</html>