我尝试创建一个使用Jasper Report 5.6.0生成PDF报告的应用程序,经过一些研究并观察youtube教程,我发现了一个问题:教程成功生成PDF文件,但我不能,它只显示空PDF :(
PS:静态文本也没有显示
JasperController.java
@Named(value = "jasperController")
@Dependent
public class JasperController {
@EJB
PelangganFacade pf;
List<Pelanggan> plg;
JasperPrint jasperPrint;
public List<Pelanggan> getPelanggan() {
plg = pf.findAll();
return plg;
}
public void setPelanggan(List<Pelanggan> pelanggan) {
this.plg = pelanggan;
}
public void init() throws JRException {
JRBeanCollectionDataSource datasource = new JRBeanCollectionDataSource(plg);
String reportPath = FacesContext.getCurrentInstance().getExternalContext()
.getRealPath("WEB-INF/Blank_A4.jasper");
jasperPrint = JasperFillManager.fillReport(reportPath, new HashMap(), datasource);
}
public void createPDF() throws JRException, IOException {
init();
HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
response.addHeader("Content-disposition", "attachment; filename=report.pdf");
ServletOutputStream outputStream = response.getOutputStream();
JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
FacesContext.getCurrentInstance().responseComplete();
}
}
JSF Faces
<h:form>
<h:commandButton value="Export PDF" action="#{jasperController.createPDF()}" />
</h:form>
Pelanggan.java的一部分(让你知道字段名称)
public class Pelanggan implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Column(name = "idPelanggan")
private Integer idPelanggan;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "nama")
private String nama;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "perusahaan")
private String perusahaan;
// @Pattern(regexp="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?", message="Invalid email")//if the field contains email address consider using this annotation to enforce field validation
@Size(max = 45)
@Column(name = "email")
private String email;
@Size(max = 45)
@Column(name = "telp")
private String telp;
@Size(max = 45)
@Column(name = "hape")
private String hape;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "alamat")
private String alamat;
Blank_A4.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version last-->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="b86c8877-f14d-4642-bf2b-910096217a3a">
<property name="com.jaspersoft.studio.data.defaultdataadapter" value="TestHar"/>
<queryString>
<![CDATA[SELECT * FROM Pelanggan]]>
</queryString>
<field name="nama" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="perusahaan" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="email" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="telp" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="hape" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="alamat" class="java.lang.String">
<fieldDescription><![CDATA[]]></fieldDescription>
</field>
<group name="nama">
<groupExpression><![CDATA[$F{nama}]]></groupExpression>
</group>
<group name="perusahaan">
<groupExpression><![CDATA[$F{perusahaan}]]></groupExpression>
</group>
<group name="email">
<groupExpression><![CDATA[$F{email}]]></groupExpression>
</group>
<group name="telp">
<groupExpression><![CDATA[$F{telp}]]></groupExpression>
</group>
<group name="hape">
<groupExpression><![CDATA[$F{hape}]]></groupExpression>
</group>
<group name="alamat">
<groupExpression><![CDATA[$F{alamat}]]></groupExpression>
</group>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="79" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="555" height="79" uuid="7bc22187-5b7a-48b9-8b6c-663442314045"/>
<textElement textAlignment="Center" verticalAlignment="Middle">
<font fontName="Arial" size="26" isBold="true"/>
</textElement>
<text><![CDATA[Static Text]]></text>
</staticText>
</band>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="61" splitType="Stretch">
<staticText>
<reportElement x="0" y="40" width="100" height="20" uuid="4c0c57c6-f434-429f-bc89-2c3a69364f1e"/>
<text><![CDATA[nama]]></text>
</staticText>
<staticText>
<reportElement x="450" y="40" width="100" height="20" uuid="2cd50524-bfec-4dbf-8bb4-7b199c8dcc14"/>
<text><![CDATA[alamat]]></text>
</staticText>
<staticText>
<reportElement x="100" y="40" width="100" height="20" uuid="4cae3344-b6f0-484e-9155-67c5e65b17ba"/>
<textElement textAlignment="Left">
<font isBold="true"/>
</textElement>
<text><![CDATA[PERUSAHAAN]]></text>
</staticText>
<staticText>
<reportElement x="350" y="40" width="100" height="20" uuid="4d24ba31-12f9-4972-b861-26e88172a717"/>
<text><![CDATA[telp]]></text>
</staticText>
<staticText>
<reportElement x="200" y="40" width="130" height="20" uuid="63ee6043-0ecc-4249-99b2-523e39a7ffea"/>
<text><![CDATA[Email]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="34" splitType="Stretch">
<textField>
<reportElement x="-1" y="0" width="100" height="20" uuid="65ae19c3-dc06-45e8-95b1-c0e00289329b"/>
<textFieldExpression><![CDATA[$F{nama}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="450" y="0" width="100" height="20" uuid="6ec9762d-3a2c-438f-a397-4e7ede9c9c6b"/>
<textFieldExpression><![CDATA[$F{alamat}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20" uuid="13856c00-f231-4170-93af-f01a0a02997c"/>
<textFieldExpression><![CDATA[$F{perusahaan}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="350" y="0" width="100" height="20" uuid="81f072ad-c0ce-490d-b380-c2c1b4c37c31"/>
<textFieldExpression><![CDATA[$F{telp}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="130" height="20" uuid="63148423-b08b-4937-b586-904a82397b88"/>
<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band splitType="Stretch"/>
</pageFooter>
<summary>
<band splitType="Stretch"/>
</summary>
</jasperReport>
调试:
是的我知道,它有null对象(空) 然后我尝试更改一些代码,使对象不空
现在显示错误消息: