PrimeFaces和JasperReports

时间:2011-02-25 08:40:04

标签: java jsf glassfish jasper-reports primefaces

我正在尝试开发一个用于报告的Web应用程序,并且已经构建了一个包含EJB3 + primefaces的站点。现在,我正在努力将Jasperreport整合到主要方面...我没有找到关于这个主题的指导或讨论,我想这是不可能的?或者实际上这不是一种正确的方法吗?我实际上是开发JavaEE6的新手,不打算使用像spring和seam这样的框架。


模型

@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();

    }
}

控制器

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

    @EJB
    private BookEJB bookEJB;

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

查看(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>

碧玉

<?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>

单击JSF按钮时发生错误,下面的日志来自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.....

4 个答案:

答案 0 :(得分:4)

您是否尝试在commandLink中使用p:fileDownload组件?

请参阅: http://www.primefaces.org/showcase/ui/fileDownload.jsf

这样,您只需要另一个控制器/托管bean方法来返回StreamedContent(primefaces类)的实例,而不是乱用responseComplete或者必须创建一个单独的servlet。

答案 1 :(得分:1)

您正在修改“responseComplete()”之后的响应。调用“responseComplete()”应该是你做的最后一件事。

答案 2 :(得分:1)

处理jasper方法的最佳方法是在commandButton / commandLink中禁用ajax。这就是如何保持贾斯珀转换。

<p:commandButton title="EK Preview" icon="fa fa-file-pdf-o" actionListener="#{ctrlFiDoc.doReport_PC}" ajax="false"/>

如果没有必要,则无需严格遵守p:fileDownload - 元素。

答案 3 :(得分:0)

这条线可能导致了这个问题:

  

InputStream reportStream = ctx.getExternalContext()   .getResourceAsStream( “报告/ report1.jasper”);

请参阅this question