Hello, I have the following situation: I have a button (search) that executes a search method and returns the data in the same already open tab in a <p:dataTable
.
After generating a report in JasperReports
(pdf button), which opens in a new tab, if I click the search button again, a new tab is also opened, and I would like the search to always be shown in the same tab.
In the bean where the methods are below the scope is @ViewScoped
.
I would like help to understand this behavior and if possible adjust the code to open the search always on the same tab.
Methodthatgeneratesreport:
publicvoidexecute()throwsParseException{JasperReportBuilderreport=DynamicReports.report();configure(report);Stringq=getQueryPrincipal(queryPrincipal)+getData(formatDataInicial(dataInicialQuery),formatDataFinal(dataFinalQuery));if(arrayCliente.size()>0){q=q+nomesParaQuery(queryNome);}if(arrayStatus.size()>0){q=q+statusParaQuery(queryStatus);}if(arrayOcorrencia.size()>0){q=q+ocorrenciasParaQuery(queryOcorrencia);}if(arrayFormasPg.size()>0){q=q+formasPgParaQuery(queryFormaPg);}if(arrayDespesa.size()>0){q=q+despesasParaQuery(queryDespesa);}report.setDataSource(q,con);System.out.println("Query: " + q);
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
try {
response.setContentType("application/pdf");
report.toPdf(response.getOutputStream());
FacesContext.getCurrentInstance().responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
}
Search method:
public List<ConsultaEntrada> teste() throws SQLException, ParseException {
List<ConsultaEntrada> result = new ArrayList<ConsultaEntrada>();
String q = "";
try {
System.out.println("ANTES:" + dataInicialQuery);
System.out.println("ANTES:" + dataFinalQuery);
// formatDataInicial(dataInicialQuery);
// formatDataFinal(dataFinalQuery);
System.out.println("Enviando:" + dataInicialQuery);
q = getQueryPrincipal(queryPrincipal)
+ getData(formatDataInicial(dataInicialQuery), formatDataFinal(dataFinalQuery));
} catch (Exception e) {
e.printStackTrace();
}
if (arrayCliente.size() > 0) {
q = q + nomesParaQuery(queryNome);
}
if (arrayStatus.size() > 0) {
q = q + statusParaQuery(queryStatus);
}
if (arrayOcorrencia.size() > 0) {
q = q + ocorrenciasParaQuery(queryOcorrencia);
}
if (arrayFormasPg.size() > 0) {
q = q + formasPgParaQuery(queryFormaPg);
}
if (arrayDespesa.size() > 0) {
q = q + despesasParaQuery(queryDespesa);
}
Statement statement = con.createStatement();
ResultSet set = statement.executeQuery(q);
set.beforeFirst();
while (set.next()) {
ConsultaEntrada consultaEntrada = new ConsultaEntrada();
consultaEntrada.setCliente(set.getString("cliente_nome"));
consultaEntrada.setOcorrencia(set.getString("ocorrencia_descricao"));
consultaEntrada.setFormaPagamento(set.getString("entrada_acidente_forma_pagamento"));
consultaEntrada.setStatus(set.getString("entrada_acidente_status"));
consultaEntrada.setDataCriacao(set.getDate("entrada_acidente_data_criacao"));
consultaEntrada.setDespesa(set.getString("despesa_nome"));
result.add(consultaEntrada);
}
con.close();
return result;
}
public void pesquisar() throws ParseException {
try {
filtradas = teste();
for (int i = 0; i < filtradas.size(); i++) {
}
System.out.println("Verificando tamanho lista" + filtradas.size());
} catch (SQLException e) {
e.printStackTrace();
}
}
xhtml:
<p:toolbar style="margin-top: 20px">
<p:toolbarGroup>
<p:commandButton action="#{simpleReport.execute}" value="PDF"
ajax="false" onclick="this.form.target='_blank'" />
</p:toolbarGroup>
<p:toolbarGroup>
<p:commandButton action="#{simpleReport.pesquisar}"
value="Pesquisar" ajax="false" />
</p:toolbarGroup>
<p:toolbarGroup>
<h:commandLink>
<p:graphicImage library="images" name="excel.png" width="30" />
<p:dataExporter type="xls" target="entradasTable"
fileName="clientesFiltrados"
postProcessor="#{customizedDocumentsView.postProcessXLS}" />
</h:commandLink>
</p:toolbarGroup>
</p:toolbar>