PrimeFaces et JasperReports

je suis en train de développer une application web pour les rapports, et ont construit un site avec EJB3 + primefaces déjà. Maintenant, je suis à la tête douleurs comment intégrer Jasperreport en primefaces... je n'ai pas trouvé de guide ou de discussion sur ce sujet, je me demande est-il pas possible? ou en fait c'est pas une bonne façon de faire? Je suis en fait assez nouveau pour développer JavaEE6, ne penses pas à utiliser des frameworks comme spring et de la couture encore.


modèle

@Stateless
@LocalBean
public class BookEJB {
    //@Override
    public void printReport() throws ClassNotFoundException, IOException, JRException, SQLException {
        Connection connection;
        Map parameterMap = new HashMap();

        FacesContext ctx = FacesContext.getCurrentInstance();

        HttpServletResponse response = (HttpServletResponse) ctx
                .getExternalContext().getResponse();

        InputStream reportStream = ctx.getExternalContext()
                .getResourceAsStream("reports/report1.jasper");

        ServletOutputStream servletOutputStream = response.getOutputStream();
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost/bookdb?user=root&password=******");

        ctx.responseComplete();
        response.setContentType("application/pdf");

        JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, parameterMap, connection);

        connection.close();
        servletOutputStream.flush();
        servletOutputStream.close();

    }
}

Contrôleur

@ManagedBean(name = "bookCtrl")
@RequestScoped
public class BookController {

    @EJB
    private BookEJB bookEJB;

    public void doPrintReport() throws ClassNotFoundException, IOException, JRException, SQLException {
        bookEJB.printReport();
    }
}

vue (JSF)

<body>
<f:view>
    <h:outputText value="Click on the link below to generate the report." />
    <h:form>
        <h:commandButton action="#{bookCtrl.doPrintReport}" value="Generate Report" />
    </h:form>
</f:view>
</body>

Jasper

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio -->
<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="report1" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<queryString language="SQL">
<![CDATA[SELECT
*
FROM
`book` book]]>
</queryString>
<field name="Id" class="java.lang.Integer"/>
<field name="title" class="java.lang.String"/>
<field name="price" class="java.lang.String"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="45" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="21" splitType="Stretch">
<staticText>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[Id]]></text>
</staticText>
<staticText>
<reportElement x="100" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[title]]></text>
</staticText>
<staticText>
<reportElement x="200" y="0" width="100" height="20"/>
<textElement/>
<text><![CDATA[price]]></text>
</staticText>
</band>
</columnHeader>
<detail>
<band height="24" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.Integer"><![CDATA[$F{Id}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="100" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{title}]]></textFieldExpression>
</textField>
<textField>
<reportElement x="200" y="0" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$F{price}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

L'erreur s'est produite lorsque je clique sur le bouton de JSF, ci-dessous du journal est de glassfish

WARNING: A system exception occurred during an invocation on EJB BookEJB method public void blah.BookEJB.printReport() throws java.lang.ClassNotFoundException,java.io.IOException,net.sf.jasperreports.engine.JRException,java.sql.SQLException
javax.ejb.EJBException.....
SEVERE: javax.ejb.EJBException
javax.faces.el.EvaluationException: javax.ejb.EJBException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
WARNING: #{bookCtrl.doPrintReport}: javax.ejb.EJBException
javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
Caused by: javax.faces.el.EvaluationException: javax.ejb.EJBException...
SEVERE: javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
Caused by: javax.faces.FacesException: #{bookCtrl.doPrintReport}: javax.ejb.EJBException...
Caused by: javax.faces.el.EvaluationException: javax.ejb.EJBException...
Caused by: javax.ejb.EJBException...
Caused by: java.lang.NullPointerException...
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
WARNING: StandardWrapperValve[Faces Servlet]: PWC1406: Servlet.service() for servlet Faces Servlet threw exception
java.lang.IllegalStateException: PWC3991: getOutputStream() has already been called for this response.....
  • comment résoudre le "java.lang.IllegalStateException: PWC3991: getOutputStream() a déjà été appelée pour cette réponse" question? je suppose que c'est glassfish problème plutôt que de Jaspe et de Primefaces maintenant...
  • Votre EJB est lancer une exception NullPointerException et qui est au cœur de votre problème ici. Quel est son stacktrace?
InformationsquelleAutor Mythox | 2011-02-25