【问题标题】:jasper pdf report does not show upjasper pdf 报告不显示
【发布时间】:2014-11-09 12:13:56
【问题描述】:

我的 JSF 2.0 Web 应用程序旨在生成 PDF 报告。 问题是资源管理器窗口中没有显示 PDF 报告。 我正在使用 eclipse 开普勒,带有 apache-tomcat-7.0.52,jasper Ireport 的版本是 4.8 任何帮助将不胜感激。

我会提供整个java类:

   package khldqr.beans;


import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;


@ManagedBean 
@SessionScoped

public class TestReport  {

    private List<Refugee> refugee;  


    public List<Refugee> connectRefugeeData() {
        ResultSet rs = null;
        PreparedStatement pst = null;
        Connection con = Database.getConnection();
        String stm = "Select R_NO, F_P_Name from M_MAIN_INFO where R_NO < 10";

        refugee = new ArrayList<Refugee>();

        try {
            pst = con.prepareStatement(stm);
            pst.execute();
            rs = pst.getResultSet();

            while (rs.next()) {
                Refugee refugeelist = new Refugee();
                refugeelist.setR_NO(rs.getInt(1));
                refugeelist.setF_P_Name(rs.getString(2));

                refugee.add(refugeelist);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return refugee;
    }

        public void PDF(ActionEvent actionEvent) throws IOException, JRException {
            System.out.println("this is not my lucky day!!!!!");
            File jasper = new File(FacesContext.getCurrentInstance().getExternalContext().getRealPath("report/Majd.jasper"));
            byte[] bytes = JasperRunManager.runReportToPdf(jasper.getPath(),null,new JRBeanCollectionDataSource(refugee));
            HttpServletResponse response =(HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
            response.setContentType("application/pdf");
            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(bytes, 0 , bytes.length);
            outputStream.flush();
            outputStream.close();
            FacesContext.getCurrentInstance().responseComplete();
        }

    public TestReport() {
    connectRefugeeData();
    }

    public List<Refugee> getRefugee() {
        return refugee;
    }

    public void setRefugee(List<Refugee> refugee) {
        this.refugee = refugee;
    }

}

这里是 xhtml 文件:

    <?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Hello To GOPA World!!</title>

</h:head>

<h:body dir="rtl">


               <h:form>
                <p:commandButton value = "PDF" actionListener="#{testReport.PDF}"></p:commandButton>
                 </h:form>
    <h:dataTable value="#{testReport.refugee}" var="var">
    <h:column>
      <h:outputText value="#{var.r_NO}"></h:outputText>
    </h:column>

    <h:column >
      <h:outputText value="#{var.f_P_Name}"></h:outputText>
    </h:column>
    </h:dataTable>

</h:body>
</html>

我可以在控制台上看到消息并且页面被刷新,但资源管理器屏幕上没有出现 PDF 报告

我已经用下面的代码替换了上面的 PDF 方法,但徒劳无功,结果相同:资源管理器屏幕上没有 PDF 报告。

   JasperPrint jasperPrint;
      public void init() throws JRException{
            JRBeanCollectionDataSource beanCollectionDataSource=new JRBeanCollectionDataSource(refugee);
    String  reportPath= "e:\\Rita.jasper";
    jasperPrint=JasperFillManager.fillReport(reportPath, new HashMap(),beanCollectionDataSource);
        }


       public void PDF(ActionEvent actionEvent) throws JRException, IOException{
           init();
           HttpServletResponse httpServletResponse=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
        //  httpServletResponse.addHeader("Content-disposition", "attachment; filename=report.pdf");
           FacesContext.getCurrentInstance().getExternalContext().setResponseContentType("‌​application/pdf");
           ServletOutputStream servletOutputStream=httpServletResponse.getOutputStream();
           JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);
           System.out.println("All done the report is done");
           servletOutputStream.flush();
           servletOutputStream.close(); 
           FacesContext.getCurrentInstance().responseComplete();
       }

【问题讨论】:

  • 这是哪种编程语言?
  • 感谢您的快速回复,很抱歉错过了这些!!。程序是eclipse,服务器是tomcat 7,jasper版本是4.8,编程语言是jsf 2.0
  • UPDATE 当我将组件 'p:commandButton' 替换为 'h:commandButton' 并将属性 'ationListener' 替换为 'action' 时,控制台消息也不会显示,这意味着未调用该操作。有什么建议么!?请..!!!

标签: pdf jsf-2


【解决方案1】:

代码是正确的,没有任何问题。 问题是某种安全问题。 当报告位于所有用户的完全访问文件夹中时,我遇到了上述问题。 当我将请求的 xhtml 和报告都放在安全文件夹中时,一切正常。 我不知道为什么!!但我就是这样。 希望其他人会利用这一点。 谢谢。

【讨论】:

    【解决方案2】:
    <p:commandButton value = "PDF" actionListener="#{testReport.PDF}" ajax="false" type="submit"></p:commandButton>
    

    调用 jasperReports 时不能使用 ajax

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      • 2015-08-14
      相关资源
      最近更新 更多