【问题标题】:Popping up a simple text message alert from a function in a java file从 java 文件中的函数弹出一个简单的文本消息警报
【发布时间】:2018-10-10 10:38:36
【问题描述】:

现在我有一个带有函数的 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 文件中弹出的正确方法,以及函数产生的文本。

【问题讨论】:

标签: javascript java html jsf


【解决方案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);
}   

【讨论】:

  • 与其创建所有的 'addInfoMessage' 方法,不如使用 OmniFaces。 showcase.omnifaces.org/utils/Messages。并更好地搜索重复...
  • 可能是个好主意,虽然我很少赞成在我的代码中添加库来解决一个问题
猜你喜欢
  • 1970-01-01
  • 2012-08-22
  • 2012-02-25
  • 2011-08-08
  • 2017-02-13
  • 2016-02-12
  • 1970-01-01
  • 2022-09-29
  • 2018-10-03
相关资源
最近更新 更多