从java文件中的函数弹出简单的文本消息警报

时间:2018-04-30 10:35:17

标签: javascript java html jsf

现在我有一个带有函数的Java文件:

public void asd() throws SQLException {

    try {
        getConnections();
    } catch (Exception ex) {
        Logger.getLogger(PSTimer.class.getName()).log(Level.SEVERE, null, ex);
    }

    MongoCursor<Document> cursor = (MongoCursor<Document>) database.getCollection("Partidoscontagem").find().sort(new Document("data", -1)).limit(1).iterator();
    if (cursor != null && cursor.hasNext()){
            ...
            ...
            ...
    } else {
        /*This is where I want to include the message "No document found"*/
    }

我的html文件中有一个p:commandButton代码:

<p:commandButton  actionListener="#{bean.asd()}">

我想知道在单击p:commandButton之后在我的html文件中执行弹出窗口的正确方法,其中包含函数产生的文本。

1 个答案:

答案 0 :(得分:1)

这对我有用。

<h:form>
    <p:growl id="growl" life="2000" />
    <p:commandButton  actionListener="#{bean.asd()}" update="growl">
</h:form>
public void asd() throws SQLException {
    if(foo) {
        // do stuff
    } else {
        addInfoMessage("Eh. That's interesting.");
    }
}


private void addInfoMessage(String summary) {
    FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);
    FacesContext.getCurrentInstance().addMessage(null, message);
}