简体   繁体   中英

Primefaces p:messages does not display

Hi I have problem with display p:messages I have this page:

<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form prependId="false">  
        <p:messages id="msgs" showDetail="true"/>  
        <p:commandButton value="Info" actionListener="#{messagesController.addInfo}" update="msgs"/>  
        <p:commandButton value="Warn" actionListener="#{messagesController.addWarn}" update="msgs"/>  
        <p:commandButton value="Error" actionListener="#{messagesController.addError}" update="msgs"/>  
        <p:commandButton value="Fatal" actionListener="#{messagesController.addFatal}" update="msgs"/>  
    </h:form> 
</h:body>

and this is my bean to manage the messages

@ManagedBean(name = "beanMessageManager")
@SessionScoped
public class BeanMessageManager {    
    public void addInfo(ActionEvent actionEvent) {  
        FacesContext.getCurrentInstance().addMessage("form1:msgs", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces rocks!"));  
    }   
    public void addWarn(ActionEvent actionEvent) {  
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,"Sample warn message", "Watch out for PrimeFaces!"));  
    }    
    public void addError(ActionEvent actionEvent) {  
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Sample error message", "PrimeFaces makes no mistakes"));  
    }  
    public void addFatal(ActionEvent actionEvent) {  
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_FATAL,"Sample fatal message", "Fatal Error in System"));  
    }   
}

When I click on some button then message isn't display.

What I'am doing bad?

Many thanks for response

Looks like you have the wrong bean name in actionListener .

The bean is named @ManagedBean(name = "beanMessageManager") but in the xhtml page you try to call actionListener="#{messagesController.addInfo}" .

Maybe this would work:

<p:commandButton value="Info" actionListener="#{beanMessageManager.addInfo}" update="msgs"/>  
<p:commandButton value="Warn" actionListener="#{beanMessageManager.addWarn}" update="msgs"/>  
<p:commandButton value="Error" actionListener="#{beanMessageManager.addError}" update="msgs"/>  
<p:commandButton value="Fatal" actionListener="#{beanMessageManager.addFatal}" update="msgs"/>

EDIT
Also you are missing the </html> tag at the end of you xhtml file. Also I think you should use msgs instead of "form1:msgs" . Unfortunately I am not 100% sure on how primefaces works since I never used it.

The problem is that here actionListener="#{messagesController.addInfo}" you are calling a messageController that may not exists. You should be using the beanMessageManager instead.

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