primefaces p:不显示消息

时间:2016-05-24 21:02:11

标签: jsf primefaces

我在表单中有一个按钮,我想在单击此按钮时显示错误消息。这是我想要在编写类似的东西之前完成的一个步骤。我的代码不起作用,因为单击按钮时不显示错误消息。

我的表格是:

<h:form id="myForm">
    <p:messages id="errorMessage" for="myForm" autoUpdate="true" />
    <p:fieldset id="myFieldSet"
                style="margin-left:auto ; margin-right:auto ; width:98% ; height:90px;">
        <p:outputLabel for="numSi"
                       value="Value :"
                       style="margin-left:31px;margin-top:25px;" />
        <p:inputText id="numSi"
                     value="#{suppSiBean.researchValue}"
                     maxlength="9" required="true">
        </p:inputText>
        <p:commandButton id="suppSignaButton" type="submit" ajax="true"
                         value="Launch"
                         style="text-align: center ; height:45px; width:150px ; margin-top:20px;"
                        action="#{suppSiBean.lancerRequete()}" />
    </p:fieldset>
</h:form>

我的豆的摘录:

public void lancerRequete() {}
    FacesContext context = FacesContext.getCurrentInstance();
    //I'm intentionally hide the values of the three following variables because it's confidential
    ResourceBundle bundle = ...
    String message = ...
    String messageFormat = ...
    FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_ERROR, messageFormat.toString(), "");
    FacesContext.getCurrentInstance().addMessage(getClientId("myForm"), facesMessage);
}

public String getClientId(String id) {
    FacesContext context = FacesContext.getCurrentInstance();
    UIViewRoot root = context.getViewRoot();
    UIComponent c = findComponent(root, id);
    LOGGER.info("c.getClientId(context) vaut :" + c.getClientId(context));
    return c.getClientId(context);
}

private UIComponent findComponent(UIComponent c, String id) {
    if (id.equals(c.getId())) {
        return c;
    }
    Iterator<UIComponent> kids = c.getFacetsAndChildren();
    while (kids.hasNext()) {
        UIComponent found = findComponent(kids.next(), id);
        if (found != null) {
            return found;
        }
    }
    return null;
}

1 个答案:

答案 0 :(得分:-1)

为什么不直接在addMessage方法中定位组件的ID?

public void lancerRequete() {}
     ...
     FacesContext.getCurrentInstance().addMessage("errorMessage", facesMessage);
}

最后,您应该通过添加update属性

来告知何时更新此组件
<p:commandButton ... update="errorMessage" />