简体   繁体   English

验证和丰富:popupPanel不起作用

[英]validation and rich: popupPanel don't work

My intention is to display a message to the user in a rich: popupPanel after insert a record in BD. 我的意图是在BD中插入记录后,以丰富的消息:popupPanel向用户显示一条消息。

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. 如果其中一个字段未通过验证,则验证消息将显示在h:messages中,并且即使我更改了该字段的内容并再次单击该按钮,页面也不会更改。

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; } 

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). 通过AJAX渲染rich:popupPanel的内容(从a4j:commandButton渲染)时,最好选择使用show属性有条件地显示它,而不是调用Javascript代码(不完整)。

So, I suggest you just add a condition for showing the popupPanel . 因此,我建议您仅添加一个显示popupPanel的条件。 Assuming that textoModal is null upon initialization, and your bean is a request scoped, you can just use something like this: 假设初始化时textoModal为null,并且bean是一个范围限定的请求,则可以使用如下所示的内容:

<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. 您可能想要在后备bean中创建一个类似于public boolean isShowTextoModal()的方法,该方法可以实现比仅检查字段是否为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 . ...并从方法inserirDizimista删除ActionEvent参数,使其成为一个操作方法,而不是actionListener

This should get things going for you. 这应该可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM