简体   繁体   中英

How to solve “p:message not working” in Primefaces?

I'm building a form that let the user enter database connection parameters. Once the parameters are typed the user can test (btnTester) if connection can be established with its parameters.

In all cases, a message is produced for the user. Here the example of a failed connection attempt from backing bean code:

(...)
addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection failed.", t.getLocalizedMessage()));
(...)

Here is the form code. I'd like the message to appear in ap:message. Unfortunately, nothing happen. No message (connection successful or not) is displayed after the button:\ Even with the global attribute set to false or true.

<h:form prependId="false">
(...)
  <h:panelGrid>                 
    <!-- Other form components here -->
    <f:facet name="footer">
      <p:commandButton
          id="btnTester"
          value="Tester"
          actionListener="#{assistantCreationSourcesBean.testerConnexionBase}"
          update="msgTester" />
          <p:message id="msgTester" for="btnTester" />
    </f:facet>
 </h:panelGrid>
</h:form>

What am I missing?

I think your problem is here:

addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection failed.", t.getLocalizedMessage()));

I'm assuming that you're calling FacesContext.addMessage() . That first parameter is the component id string. If you set it to null, then you force the message to be a global message. The <p:message> component you have defined is set to for="btnTester" so it will only display messages with a component id that matches the id of your btnTester component.

Except from the Javadoc for addMessage() :

Append a FacesMessage to the set of messages associated with the specified client identifier, if clientId is not null. If clientId is null, this FacesMessage is assumed to not be associated with any specific component instance.

Link to FacesMessage Javadoc for addMessage() method

You are missing an update attribute on your <p:commandButton> that specifies the ID of the <p:message> component to update.

You should give the message component and ID and specify it in update of the commandButton.

I use a growl instead of a message for solving my problem.

Found a solution that works Perfectly with message in Prime face.

Thought Sharing with you even if u worked it out with growl

Here is the solution

The add addMessage has 2 parameters, a UIComponent Client Id and FacesMessage. The facesMessage shows or defines the message and Component Child Id shows where the component is. Here you have missed the Component Part. In-order to make it work you should bind the UIComponent with the bean class. So that it can find the Component.

Now do as follows,

Define a UIComponent in bean class

Bind it with the corresponding command Button

and get the Id From the component in the bean Class

Your Code must be changed like this

>  private UIComponent component;

(...)
addMessage(component.getClientId(), new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Connection failed.", t.getLocalizedMessage()));
(...)

Inside the xhtml file you must bind the UIComponent

<h:form prependId="false">
(...)
  <h:panelGrid>                 
    <!-- Other form components here -->
    <f:facet name="footer">
      <p:commandButton
          id="btnTester"
          value="Tester"
          actionListener="#{assistantCreationSourcesBean.testerConnexionBase}"
          update="msgTester" 
          binding="#{assistantCreationSourcesBean.component}"/>
          <p:message id="msgTester" for="btnTester" />
    </f:facet>
 </h:panelGrid>
</h:form>

Now the message will work..:! For me it worked perfectly :)

Here "for" attribute won't work for p:message use p:messages instead. <p:messages id="txtname" showIcon="false" showDetail="true" showSummary="false" redisplay="false" for="someKey" display="text"/>

I have encountered this problem recently with PrimeFaces 6.0.19.

It turns out that I must have to call FacesContext.getCurrentInstance().validationFailed() before I call the addMessage() method so the message can be properly shown.

ie

FacesContext.getCurrentInstance().validationFailed();
//if you set the client ID to be null, you should add globalOnly=true in <p:messages>
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Connection failed.", t.getLocalizedMessage()));

My guess is that you try to update a component that not yet being render. Try this

Use Firefox Firebug and check the html source to see if component id msgTester exist. If it is not then it explain why your update wont work. You cant update something that it is not there. How to fix this, well you can create a wrapper for this, and update your wrapper. Your wrapper should be something that it is always there, like h:form . IF you dont want to update the whole form, then you can try <p:outputPanel id="wrapper"> . so your code would look like this

<h:form>
    ...
    <p:outputPanel id="wrapper">
        <p:commandButton ... update="wrapper"/>
        <p:message />
    </p:outputPanel>
</h:form>

try this and let me know if it work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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