How to solve "p: message not working" in "Primefaces"?

I am creating a form that allows the user to enter database connection parameters. After entering the parameters, the user can check (btnTester) if the connection can be established with its parameters.

In all cases, a message is created for the user. Here is an example of a failed connection attempt using bean code:

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

Here is the form code. I want the message to appear in the message p :. Unfortunately, nothing happens. After pressing the button: \ n the message does not appear (connection is successful or not) Even if the global attribute is 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?

+7
source share
6 answers

I use a growl instead of a message to solve my problem.

+2
source

I think your problem is here:

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

I assume that you are calling FacesContext.addMessage() . This first parameter is the identifier string of the component. If you set it to null, then you force the message to be a global message. The <p:message> component that you defined is set to for="btnTester" , so it will display messages with the identifier of the component that matches the identifier of your btnTester component.

Except for Javadoc for addMessage() :

Add FacesMessage to the message set associated with the specified client identifier, if clientId is not null. If clientId is null, it is assumed that this FacesMessage is not associated with any particular component instance.

FacesMessage Javadoc reference for addMessage () method

+4
source

Your <p:commandButton> missing the update attribute, which indicates the updated identifier of the <p:message> component.

You must specify the message component and identifier and specify it in update in commandButton.

+3
source

Find a solution that works great with the message in Prime face.

Thinking with you, even if you worked with a growl

Here is the solution

There are 2 parameters in add addMessage, the client identifier is UIComponent and FacesMessage. FaceMessage shows or identifies the message, and the identifier of the child element of the component shows where the component is located. Here you skipped the component part. To do this, you must associate the UIComponent with the bean class. So that he can find the Component.

Now do the following:

Define a UIComponent in a bean class

Bind it with the appropriate Button command

and get the identifier from the component in the bean class

Your code should be modified as follows

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

Inside the xhtml file you have to bind 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 fine :)

+1
source

Here the โ€œforโ€ attribute will not work for p: message use p: messages instead. <p:messages id="txtname" showIcon="false" showDetail="true" showSummary="false" redisplay="false" for="someKey" display="text"/>

+1
source

I assume that you are trying to update a component that is not yet displayed. try it

Use Firefox Firebug and check the html source to see if msgTester component msgTester . If this is not the case, then this explains why your update does not work. You cannot update what is not there. To fix this, you can create a wrapper for this and update your wrapper. Your wrapper should be what it always is, like h:form . If you do not want to update the entire form, you can try <p:outputPanel id="wrapper"> . so your code will 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 works.

0
source

All Articles