Seguinte, eu preciso fazer uma pesquisa em que ao clicar no botão eu mando um parametro para a url com o que a pessoa digitar e faço a pesquisa ao entrar na pagina novamente.
Eu fiz com um p:button, só que quando ele manda para a propria pagina novamente, ele manda o parametro como nulo. Alguem tem alguma ideia de resolver isso?
meu Bean:
package br.edu.dmsoftware.tcc.bean;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Model;
import javax.faces.event.ValueChangeEvent;
import javax.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import br.edu.dmsoftware.tcc.dao.AnuncioDao;
import br.edu.dmsoftware.tcc.dao.FavoritoDao;
import br.edu.dmsoftware.tcc.infra.Mensagens;
import br.edu.dmsoftware.tcc.modelo.Anuncio;
import br.edu.dmsoftware.tcc.modelo.Favorito;
@Model
@ViewScoped
public class PesquisaBean implements Serializable{
private String pesquisa = new String();
@Inject
private Anuncio anuncio;
@Inject
private AnuncioDao anuncioDao;
private List<Anuncio> anuncios;
@Inject
private Favorito favorito;
@Inject
private FavoritoDao favoritoDao;
@Inject
private UsuarioLogadoBean usuarioLogado;
public void buscarAnuncios(){
if(this.pesquisa != null){
this.anuncios = anuncioDao.pesquisarPorTitulo(pesquisa);
}
}
public boolean teste(){
return false;
}
public void carregarAnuncios(){
anuncios = anuncioDao.pesquisarPorTitulo(pesquisa);
}
public List<Anuncio> autoCompleteAnuncio(String pesquisa){
System.out.println(pesquisa);
anuncios = anuncioDao.pesquisarPorTitulo(pesquisa);
return anuncioDao.pesquisarPorTitulo(pesquisa);
}
@Transactional
public String adicionarFavorito(Anuncio anuncio){
if(usuarioLogado.isLogado()){
if(favoritoDao.favoritoExiste(anuncio, usuarioLogado.getUsuario())){
new Mensagens().anuncioJaFavoritado();
return "";
}
favorito.setAnuncioFavoritado(anuncio);
favorito.setDataAdicionado(Calendar.getInstance());
favorito.setUsuario(usuarioLogado.getUsuario());
try {
favoritoDao.salvar(favorito);
new Mensagens().anuncioAddAosFavoritos();
return "";
} catch (Exception e) {
new Mensagens().erro();
return "";
}
}else{
new Mensagens().usuarioNaoLogado();
return "";
}
}
public String getPesquisa() {
return pesquisa;
}
public void setPesquisa(String pesquisa) {
this.pesquisa = pesquisa;
}
public Anuncio getAnuncio() {
return anuncio;
}
public void setAnuncio(Anuncio anuncio) {
this.anuncio = anuncio;
}
public List<Anuncio> getAnuncios() {
return anuncios;
}
public void setAnuncios(List<Anuncio> anuncios) {
this.anuncios = anuncios;
}
}
Minha view:
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<f:facet name="first">
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</f:facet>
<h:outputStylesheet library="algaworks" name="styles/custom.css" />
<h:outputStylesheet library="algaworks" name="styles/layout.css" />
<h:outputStylesheet library="algaworks" name="styles/components.css" />
<h:outputScript target="body" library="primefaces"
name="jquery/jquery.js" />
<h:outputScript target="body" library="algaworks"
name="javascripts/app.js" />
<h:outputStylesheet library="sweetAlert" name="dist/sweetalert.css" />
<h:outputScript library="sweetAlert" name="dist/sweetalert.min.js" />
<h:outputStylesheet library="bootstrap" name="bootstrap.min.css" />
<h:outputScript library="bootstrap" name="bootstrap.min.js" />
<h:outputStylesheet library="dm" name="style.css" />
<h:outputLink rel="stylesheet"
hreflang="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.4.0/css/font-awesome.min.css" />
<style type="text/css">
.ui-growl{
top: 10% !important;
}
</style>
<title>Home</title>
</h:head>
<h:body>
<div id="geral" class="ui-g">
<header class="aw-topbar">
<div id="barra-superior" class="ui-g-12 ui-md-10 ui-g-nopad">
<div class="ui-g">
<h:form id="form-barra-superior">
<p:menubar autoDisplay="true">
<p:menuitem>
<a href="#" class="aw-toggle js-toggle"><i
class="fa fa-bars"></i></a>
</p:menuitem>
<p:menuitem value="Home" />
<f:facet name="options">
<p:commandButton value="#{templateBean.txtBtnLogin}"
styleClass="btn_login" action="#{templateBean.deslogar}" />
</f:facet>
</p:menubar>
</h:form>
</div>
</div>
</header>
<aside class="aw-sidebar js-sidebar">
<div id="menu-lateral" class="ui-g-12 ui-md-2">
<h:form id="form-menu-lateral">
<br />
</h:form>
</div>
</aside>
<section class="aw-content js-content">
<div id="conteudo">
<f:metadata>
<f:viewParam name="pesquisa" value="#{pesquisaBean.pesquisa}"></f:viewParam>
<f:event listener="#{pesquisaBean.buscarAnuncios}" type="preRenderView"></f:event>
</f:metadata>
<div class="ui-fluid">
<p:growl id="message" life="1600" />
<h:form>
<p:defaultCommand target="btnPesquisar"/>
<p:panelGrid layout="grid" styleClass="ui-panelgrid-blank"
columns="2" columnClasses="ui-grid-col-6, ui-grid-col-2">
<p:inputText value="#{pesquisaBean.pesquisa}" >
</p:inputText>
<p:button id="btnPesquisar" value="Pesquisar" update="@form">
<f:param name="pesquisa" value="#{pesquisaBean.pesquisa}"/>
</p:button>
</p:panelGrid>
<p:dataScroller var="anuncio" value="#{pesquisaBean.anuncios}" chunkSize="3"
id="dataScrollerAnuncios" >
<f:facet name="loader">
<p:commandButton type="button" value="Mostrar mais" icon="ui-icon-circle-triagle-s" styleClass=""/>
</f:facet>
<h:inputHidden value="#{anuncio.id}" />
<p:panelGrid columns="1" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-12">
<p:panel>
<p:panelGrid columns="2" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-12">
<p:panelGrid columns="1" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-12">
<p:link id="cmdVerDetalhes" value="#{anuncio.titulo}" outcome="/anuncio.jsf" >
<f:param name="id" value="#{anuncio.id}"/>
<f:param name="title" value="#{anuncio.titulo}"/>
</p:link>
</p:panelGrid>
</p:panelGrid>
<p:panelGrid columns="1" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-12">
<p:panelGrid columns="3" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-1, ui-grid-col-10, ui-grid-col-1">
<h:outputText value="Reputação" />
<p:rating value="#{anuncio.reputacao}" readonly="true" id="reputacao"/>
<p:commandLink id="cmdFavoritos" update="message" action="#{pesquisaBean.adicionarFavorito(anuncio)}">
<i class="fa fa-fw fa-heart" style="font-size: 23px; color: #695CF2;"/>
</p:commandLink>
</p:panelGrid>
</p:panelGrid>
<p:panelGrid columns="1" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-8">
<p:panelGrid columns="4" styleClass="ui-panelgrid-blank" layout="grid" columnClasses="ui-grid-col-1, ui-grid-col-1, ui-grid-col-1, ui-grid-col-1">
<p:commandLink id="boleto" rendered="#{anuncio.boleto}">
<i class="fa fa-fw fa-barcode" style="font-size: 23px;"/>
</p:commandLink>
<p:commandLink rendered="#{anuncio.cartaoCredito}" id="credito">
<i class="fa fa-fw fa-credit-card" style="font-size: 23px;"/>
</p:commandLink>
<p:commandLink id="cartaoDebito" rendered="#{anuncio.cartaoDebito}">
<i class="fa fa-fw fa-cc" style="font-size: 23px;"/>
</p:commandLink>
<p:commandLink id="dinheiro" rendered="#{anuncio.dinheiro}" >
<i class="fa fa-fw fa-money" style="font-size: 23px;"/>
</p:commandLink>
</p:panelGrid>
</p:panelGrid>
<p:tooltip for="cmdFavoritos" value="Adicionar aos favoritos"/>
<p:tooltip for="cmdVerDetalhes" value="Visualizar este anúncio"/>
<p:tooltip for="dinheiro" value="Dinheiro"/>
<p:tooltip for="boleto" value="Boleto"/>
<p:tooltip for="cartaoDebito" value="Cartão débito"/>
<p:tooltip for="credito" value="Crédito"/>
<p:blockUI trigger="cmdFavoritos" block="cmdFavoritos"/>
</p:panel>
</p:panelGrid>
</p:dataScroller>
</h:form>
</div>
</div>
</section>
<div id="footer" class="ui-g-12">
<ui:insert name="footer" />
</div>
</div>
</h:body>
</html>