Reputation: 15
My intention is to display a message to the user in a rich: popupPanel after insert a record in BD.
With the code below I get to do what I want, but with a problem. Only works when I type all page fields correctly. If one of the fields does not pass in validation, the validation message is displayed in the h:messages, and the page does not change, even if I change the contents of the field and click the button again.
What could be wrong?
page
<h:outputLabel value="Nome:" for="nome1" />
<h:inputText id="nome1" maxlength="50" required="true" requiredMessage="Informe um nome" immediate="true"/>
<h:outputLabel value="Logradouro:" for="logradouro" />
<h:inputText id="logradouro" maxlength="50" required="true" requiredMessage="Informe o logradouro" immediate="true"/>
<h:outputLabel value="Bairro:" for="bairro" />
<h:inputText id="bairro" maxlength="45" required="true" requiredMessage="Informe o bairro" immediate="true" />
<h:outputLabel value="CEP:" for="cep" />
<h:inputText id="cep" maxlength="8" required="true" requiredMessage="Informe o CEP" immediate="true">
<f:convertNumber integerOnly="true" groupingUsed="false" maxIntegerDigits="8" />
</h:inputText>
<a4j:commandButton id="btnInserirDizimista" value="Inserir" oncomplete="#{rich:component('pnlOk')}.show()" render="pnlMessages outPnlOk" actionListener="#{dizimistaMB.inserirDizimista}"/>
<a4j:outputPanel id="outPnlOk" ajaxRendered="true">
<rich:popupPanel id="pnlOk" modal="true" height="150">
<h:graphicImage value="/images/info.jpg" />
<h:outputText id="textoModalOk" value="#{dizimistaMB.textoModal}" />
<h:commandButton value="OK" action="dizimista?faces-redirect=true"
onclick="#{rich:component('pnlOk')}.hide(); return false;" >
<a4j:ajax execute="formPnlOk" />
</h:commandButton>
</rich:popupPanel>
</a4j:outputPanel>
managed bean
public void inserirDizimista(){
try{
defineDizimista("formNovoDizimista");
dizimistaFacade.criar(dizimista);
setTextoModal("Dizimista inserido com sucesso");
System.out.println("Dizimista '" + dizimista.getNome1() + "' inserido com sucesso");
dizimista = null;
} catch (DAOException e) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, e.getMessage(), null);
JSFHelper.getFacesContext().addMessage("ATENÇÃO!", msg);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
private void defineDizimista(String nomeForm){
Map<String, String> dados = getDadosFormulario(nomeForm);
dizimista.setNome1(dados.get("nome1"));
dizimista.setLogradouro(dados.get("logradouro"));
dizimista.setBairro(dados.get("bairro"));
dizimista.setCep(Integer.parseInt(dados.get("cep")));
}
private Map<String, String> getDadosFormulario(String form){
Map<String, String> dados = new HashMap<String, String>();
dados.put("nome1", JSFHelper.getRequestParameter(form +
UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "nome1"));
dados.put("logradouro", JSFHelper.getRequestParameter(form +
UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "logradouro"));
dados.put("bairro", JSFHelper.getRequestParameter(form +
UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "bairro"));
dados.put("cep", JSFHelper.getRequestParameter(form +
UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "cep"));
dados.put("cidade", JSFHelper.getRequestParameter(form +
UINamingContainer.getSeparatorChar(JSFHelper.getFacesContext()) + "cidade"));
return dados;
}
Upvotes: 0
Views: 3327
Reputation: 23806
When you render the contents of a rich:popupPanel
through AJAX (rendering from a4j:commandButton
), it may be best choose to display it conditionally using its show
attribute, instead of calling Javascript code (oncomplete).
So, I suggest you just add a condition for showing the popupPanel
. Assuming that textoModal
is null upon initialization, and your bean is a request scoped, you can just use something like this:
<rich:popupPanel id="pnlOk" modal="true"
show="#{dizimistaMB.textoModal != null}" ...>
You may want to create a method in the backing bean something like public boolean isShowTextoModal()
that may implement better rules than simply checking if a field is not null.
Then, you can change your button to this:
<h:commandButton id="btnInserirDizimista" value="Inserir"
render="pnlMessages outPnlOk"
action="#{dizimistaMB.inserirDizimista}" />
... and remove the ActionEvent
argument from your method inserirDizimista
, making it an action method instead of a actionListener.
This should get things going for you.
Upvotes: 1