Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 a p: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 ?

like image 793
Stephan Avatar asked Jul 13 '11 15:07

Stephan


3 Answers

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.

like image 88
maple_shaft Avatar answered Oct 22 '22 01:10

maple_shaft


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

like image 4
Jim Tough Avatar answered Oct 22 '22 01:10

Jim Tough


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

like image 3
Stephan Avatar answered Oct 22 '22 03:10

Stephan