2016-02-17 52 views
0

我有一種方法,其添加消息這樣一個managed bean:FacesContext.addMessage()不會在p顯示消息:消息

@ManagedBean 
@ViewScoped 
public class FileMB implements Serializable{ 

private static final long serialVersionUID = -2843716026642626725L; 

private UploadedFile file; 
private boolean enabled = false; 

public UploadedFile getFile() { 
    return file; 
} 

public void setFile(UploadedFile file) { 
    this.file = file; 
} 

public void upload() { 

    FacesMessage message = new FacesMessage("test"); 
    FacesContext.getCurrentInstance().addMessage(null, message); 

} 

我把這種方法從XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:p="http://primefaces.org/ui"> 
<h:head> 
    <title></title> 
</h:head> 
<h:body> 
<h:form> 
    <p:commandButton value="Show" type="button" actionListener="#{fileMB.upload}" /> 
</h:form> 
<h:form id="frmMensajes"> 
    <p:messages id="mensajes" showDetails="false"/> 
</h:form> 
</h:body> 
</html> 

但我不能看到「測試」消息,我也在faces-config中添加了託管bean聲明,我該怎麼辦?

回答

0

嘗試添加autoUpdate屬性。

事情是這樣的:<p:messages id="mensajes" showDetails="false" autoUpdate="true" />

根據文檔:

啓用自動更新,郵件組件與每個Ajax請求自動更新

+0

還有一個原因,但OP會發現,自己曾經他的數字採取行動方法實際上不是調用(否則它只是在這個問題作爲一個紅鯡魚擺弄結果)。 – BalusC

+0

謝謝你所有的貢獻者,我只需要添加自動更新,它完美的工作!再次嘗試。我忘了給方法添加圓括號actionListener =「#{fileMB.upload()}」Primefaces不會在沒有它們的情況下工作 – Oscarmageddon

-1

請嘗試以下之一里面第一個h:form

1)p:messages globalOnly="true"

2)p:message for=<command button id>並添加id到commanbutton

+0

對不起,這兩個建議都沒有意義。下一次首先測試自己,然後再作爲答案。 – BalusC