Ireport Tibco - Error in XML processing: no element found

Asked

Viewed 830 times

1

Hello !!

I’m having trouble generating the report ,I’m using jsf,Hibernate,mysql

follow my bean.

@Named
@RequestScoped
public class RelatorioBean implements Serializable {


    private static final long serialVersionUID = 1L;

    private List<Funcionario> listaFuncionariosRel;
    private Funcionario funcionarioSelecionadosPDF;

    @Inject
    private Funcionarios funcionarios;

    @Inject
    private EntityManager manager;//

    @Inject
    private FacesContext facesContext;//

    @Inject
    private HttpServletResponse response;//

    public Funcionarios getFuncionarios() {
        return funcionarios;
    }

    public void setFuncionarios(Funcionarios funcionarios) {
        this.funcionarios = funcionarios;
    }

    public List<Funcionario> getListaFuncionariosRel() {
        return listaFuncionariosRel;
    }

    public void setListaFuncionariosRel(List<Funcionario> listaFuncionariosRel) {
        this.listaFuncionariosRel = listaFuncionariosRel;
    }

    public Funcionario getFuncionarioSelecionadosPDF() {
        return funcionarioSelecionadosPDF;
    }

    public void setFuncionarioSelecionadosPDF(Funcionario funcionarioSelecionadosPDF) {
        this.funcionarioSelecionadosPDF = funcionarioSelecionadosPDF;
    }

    public void exporterPdf() {//não estou usando este..método
        try {

            FacesContext facesContext = FacesContext.getCurrentInstance();
            ExternalContext ec = facesContext.getExternalContext();
            HttpServletResponse response = (HttpServletResponse) ec.getResponse();

            //InputStream caminho = getClass().getResourceAsStream("/Ireport/func.jrxml");//acrescentei
            Map<String, Object> params = new HashMap<>();//.getCpf()

            params.put("cpf_Funcionario", funcionarioSelecionadosPDF.getCpf());

            /*response.reset();
            response.setContentType("application/pdf");//tbm
            response.setHeader("Content-disposition", "inline; filename=relatorio.pdf");*/
            ec.responseReset();
            ec.setResponseContentType("application/pdf");
            ec.setResponseHeader("Content-disposition", "inline; filename=relatorio.pdf");

            OutputStream saida = ec.getResponseOutputStream();

            GeradorDeRelatorios gerador = new GeradorDeRelatorios();
            gerador.geraPDF("/Ireport/func.jrxml", params, saida);

            JasperReport pathReport = JasperCompileManager.compileReport("/Ireport/func.jrxml");

            saida.close();
            facesContext.getResponseComplete();

        } catch (IOException ex) {
            FacesUtil.addErrorMessage("Erro 1: " + ex.getMessage());
        } catch (JRException ex) {
            FacesUtil.addErrorMessage("Erro 2: " + ex.getMessage());
        }
    }

    public void exportPdf2() {

        Map<String, Object> params = new HashMap<>();
        //params.put("cpf",this.listarDadosRel());
        params.put("cpf_Funcionario", this.funcionarioSelecionadosPDF);
        ExecutorReport execute = new ExecutorReport("/Ireport/func.jasper",
                this.response, params, "Funcionario.pdf");
        Session session = manager.unwrap(Session.class);
        session.doWork(execute);
        facesContext.responseComplete();

    }

}

My other class Executorreport

public class ExecutorReport implements Work {

    private String caminhoReport;
    private HttpServletResponse response;
    private Map<String,Object>params;
    private String nomeArquivo;

    public ExecutorReport(String caminhoReport, HttpServletResponse response, Map<String, Object> params, String nomeArquivo) {
        this.caminhoReport = caminhoReport;
        this.response = response;
        this.params = params;
        this.nomeArquivo = nomeArquivo;
    }



    @Override
    public void execute(Connection connection) throws SQLException {
        try{
        InputStream relatorioStream = this.getClass().getResourceAsStream(nomeArquivo);        
        JasperPrint print = JasperFillManager.fillReport(relatorioStream,this.params,connection);

        JRExporter  exportador = new JRPdfExporter();

        exportador.setParameter(JRExporterParameter.OUTPUT_STREAM,response.getOutputStream());
        exportador.setParameter(JRExporterParameter.JASPER_PRINT,print);

        response.setContentType("application/pdf");
        exportador.exportReport();

    }catch(Exception e ){
        throw new SQLException("erro ao gerar "+this.caminhoReport);
    }

    }

}

apache message

Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers @Default
  • is coming up in the apache log... com.sun.faces.context.ExceptionHandlerImpl.log getOutputStream() has already been called for this response&#xA; java.lang.IllegalStateException: getOutputStream() has already been called for this response

2 answers

1


He is understanding that his output is xhtml, should be PDF: response.setContentType("application/pdf");

And there should also be a header for your output:

            response.setHeader(
                "Content-disposition",
                "inline;filename="teste.pdf");

I have no way to test its implementation, but follow a full example:

try {
        JasperDesign jasperDesign = JRXmlLoader.load(inputStream);
        JasperReport jasperReport = JasperCompileManager
                .compileReport(jasperDesign);
        JasperPrint jasperPrint = JasperFillManager.fillReport(
                jasperReport, parameters, conection);
        // Gera saida em PDF em uma servlet
        HttpServletResponse response = (HttpServletResponse) context
                .getExternalContext().getResponse();
        response.setContentType("application/pdf");
        Calendar gc = GregorianCalendar.getInstance();
        response.setHeader(
                "Content-disposition",
                "inline;filename=" + report + gc.get(Calendar.YEAR) + ""
                        + gc.get(Calendar.MONTH) + ""
                        + gc.get(Calendar.DAY_OF_MONTH) + ""
                        + gc.get(Calendar.HOUR_OF_DAY) + ""
                        + gc.get(Calendar.MINUTE) + ""
                        + gc.get(Calendar.SECOND) + ".pdf");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Pragma", "No-cache");
        ServletOutputStream servletOutputStream = response
                .getOutputStream();

        JasperExportManager.exportReportToPdfStream(jasperPrint,
                servletOutputStream);

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



    } catch (JRException ex) {
        throw new SystemException(ex);
    }
    context.responseComplete();
  • Norivan.. I followed this same example... that you put and the application now it doesn’t even go up :/ apache message Caused by: org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers @Default What I do ??

  • aew when I take imports @Inject&#xA;private EntityManager manager ``@Inject&#xA;private FacesContext facesContext;``@Inject&#xA;private HttpServletResponse response; and when I call the method exporterPdf() aew the application goes up ... and up list ... but does not export to pdf displays this message ... getOutputStream() has already been called for this response in the browser

  • are these problems...one i follow the example of algaworks that would be exportPdf2 void() that the application does not go up.. the other one even goes up but does not export...

  • the mistake is this .... WELD-001408: Unsatisfied dependencies for type FacesContext with qualifiers

  • I was able to change the dependencies of faceContext -- the error changed now displays this ... Caused by: javax.faces.el.EvaluationException: org.hibernate.exception.GenericJDBCException: error executing work my report path I’m calling right. What could be ?

  • Looks like DBMS closed the connection. Show all Stack

Show 1 more comment

0

Aew caught ...I created a new class in the . util package

public class UtilFaces {

public UtilFaces() {
}
public FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
}
public ExternalContext getExternalContext() {
    return getFacesContext().getExternalContext();
}
public HttpServletRequest getRequest() {
    return (HttpServletRequest) getExternalContext().getRequest();
}
public HttpServletResponse getResponse() {
    return (HttpServletResponse) getExternalContext().getResponse();
}
public HttpSession getHttpSession(){
    return (HttpSession) getFacesContext().getExternalContext().getSession(false);
}
public ServletContext getServletContext(){
    return (ServletContext) getExternalContext().getContext();
}
public void msg(String destination,String msg){
    FacesMessage message = new FacesMessage(msg);
    getFacesContext().addMessage(destination, message);
}
public static void addErrorMessage(Exception ex, String defaultMsg) {
    String msg = ex.getLocalizedMessage();
    if (msg != null && msg.length() > 0) {
        addErrorMessage(msg);
    } else {
        addErrorMessage(defaultMsg);
    }
}
public static void addErrorMessages(List<String> messages) {
    for (String message : messages) {
        addErrorMessage(message);
    }
}
public static void addErrorMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, msg);
    FacesContext.getCurrentInstance().addMessage(null, facesMsg);
}
public static void addSuccessMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg);
    FacesContext.getCurrentInstance().addMessage("successInfo", facesMsg);
}
public static void addWarnMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_WARN, msg, msg);
    FacesContext.getCurrentInstance().addMessage("warnInfo", facesMsg);
}
public static void addFatalMessage(String msg) {
    FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, msg, msg);
    FacesContext.getCurrentInstance().addMessage("fatalInfo", facesMsg);
}

}

and tbm updated the method calling the report ...

aew the class that did catch

public class ExecutorReport implements Work {

private String caminhoRelatorio;
private HttpServletResponse response;
private Map<String,Object>parametros;
private String nomeArquivoSaida;

private boolean relatorioGerado;

public ExecutorReport(String caminhoRelatorio, HttpServletResponse response, Map<String, Object> parametros, String nomeArquivoSaida) {
    this.caminhoRelatorio = caminhoRelatorio;
    this.response = response;
    this.parametros= parametros;
    this.nomeArquivoSaida = nomeArquivoSaida;

    this.parametros.put(JRParameter.REPORT_LOCALE, new Locale("pt", "BR"));
}



@Override
public void execute(Connection connection) throws SQLException {
    try{
    InputStream relatorioStream = this.getClass().getResourceAsStream(this.caminhoRelatorio);    
    JasperPrint print = JasperFillManager.fillReport(relatorioStream,this.parametros,connection);
     this.relatorioGerado = print.getPages().size()>0;

      if(this.relatorioGerado){
          Exporter<ExporterInput,PdfReportConfiguration,PdfExporterConfiguration,
                  OutputStreamExporterOutput> exportador = new JRPdfExporter();
                  exportador.setExporterInput(new SimpleExporterInput(print));
                  exportador.setExporterOutput(new SimpleOutputStreamExporterOutput(response.getOutputStream()));

                  response.setContentType("application/pdf");
                  response.setHeader("Content-Disposition","attachment; filename=\""
                  +this.nomeArquivoSaida + "\"");
                  exportador.exportReport();

} } catch (Exception e) { throw new Sqlexception("Error executing report " + this.wayRelatory, and); } }

public boolean isRelatorioGerado() {
    return relatorioGerado;
}

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.