2
Hello, I have the following situation: I have a button (search) that executes a search method and returns the data in the same tab already opened in a <p:dataTable
.
After generating a report on 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 are the methods below the scope is @ViewScoped
.
I would like help to understand this behavior and if possible adjust the code to open the search always in the same tab.
Method generating the report:
public void execute() throws ParseException {
JasperReportBuilder report = DynamicReports.report();
configure(report);
String q = 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();
}
}
Research 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>
I’m not sure what I’m going to say, but if you use target="_self"? also consider using _Parent because there is a difference the _parent opens in the same Frameset, also have to see if it works on prime if you don’t have to use the JSF ex: ;
<p:toolbarGroup>
 <p:commandButton action="#{simpleReport.pesquisar}" value="Pesquisar" ajax="false" onclick="this.form.target='_self'"/>
</p:toolbarGroup>
– Dilnei Cunha
Hello, it worked with your suggestion :
<p:toolbarGroup> <p:commandButton action="#{simpleReport.pesquisar}" value="Pesquisar" ajax="false" onclick="this.form.target='_self'"/> </p:toolbarGroup>
. Had tried thetarget="_self"
, but not like your example. thank you.– Rodrigo
What I didn’t understand is why, if I only clicked on the search it always opened in the same tab the search, but if before I clicked on the button that generates the pdf and then clicked on the search it opened the search in a new tab.
– Rodrigo
Probably because of the page scope, once added the HTML tree was valid for the other requests.
– Dilnei Cunha