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
 java.lang.IllegalStateException: getOutputStream() has already been called for this response
– Ricardo Mendes