【问题标题】:PrimeFaces and JasperReportsPrimeFaces 和 JasperReports
【发布时间】:2011-07-04 03:56:45
【问题描述】:

我正在尝试开发一个用于报告的 Web 应用程序,并且已经使用 EJB3 + primefaces 构建了一个站点。现在我很头疼如何将 Jasperreport 集成到 primefaces 中......我没有找到关于这个主题的指南或讨论,我想知道这不可能吗?或者实际上这不是正确的方法?我实际上是开发 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.....

【问题讨论】:

  • 如何解决“java.lang.IllegalStateException: PWC3991: getOutputStream() has been called for this response”问题?我想现在是 glassfish 问题而不是 Jasper 和 Primefaces...
  • 您的 EJB 正在抛出 NullPointerException,这是您的问题的核心。它的堆栈跟踪是什么?

标签: java jsf glassfish jasper-reports primefaces


【解决方案1】:

处理 jasper-methods 的最佳方法是在 commandButton/commandLink 中禁用 ajax。这就是保持 Jasper 转换的可能方式。

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

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

【讨论】:

    【解决方案2】:

    这行可能是导致问题的原因:

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

    this question

    【讨论】:

      【解决方案3】:

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

      见: http://www.primefaces.org/showcase/ui/fileDownload.jsf

      这样,您不必搞乱 responseComplete 或制作单独的 servlet,您只需拥有另一个控制器/托管 bean 方法,该方法返回 StreamedContent(primefaces 类)的实例。

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 2013-11-03
          • 2012-03-15
          • 2010-12-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-15
          • 1970-01-01
          • 2015-06-08
          相关资源
          最近更新 更多