Primefaces p:消息不显示

时间:2013-12-10 12:29:36

标签: jsf primefaces messages

嗨我显示p:消息有问题 我有这个页面:

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

这是我管理消息的bean

@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"));  
    }   
}

当我点击某个按钮时,不显示消息。

我做得不好?

非常感谢您的回复

2 个答案:

答案 0 :(得分:1)

看起来actionListener中有错误的bean名称。

该bean名为@ManagedBean(name = "beanMessageManager"),但在xhtml页面中,您尝试调用actionListener="#{messagesController.addInfo}"

也许这会奏效:

<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"/>

编辑
您还缺少xhtml文件末尾的</html>标记。另外我认为您应该使用msgs而不是"form1:msgs"。不幸的是,由于我从未使用它,所以我不能100%确定primefaces是如何工作的。

答案 1 :(得分:0)

问题在于actionListener="#{messagesController.addInfo}"您正在调用可能不存在的 messageController 。您应该使用 beanMessageManager