我遇到以下问题-我尝试将一些错误消息从后端bean添加到我的JSF页面。此消息与客户端ID一起添加。当我尝试添加不带客户端ID的消息时,一切正常。我必须补充说,同一bean被多个页面使用。该页面如下所示:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
template="/WEB-INF/templates/blank.xhtml">
<ui:define name="content">
<p:messages id="messages" showDetail="true" closable="true">
<p:autoUpdate/>
</p:messages>
<h:form id="changePassword" pt:autocomplete="off">
<h:panelGroup layout="block" styleClass="dialogForm">
<h:panelGrid styleClass="datatable">
<h:panelGroup>
<p:password id="oldpassword" value="#{ChangePwdBean.oldPassword}"
styleClass="requested" redisplay="false" required="true"
tabindex="1" />
<p:message for="oldpassword"/>
</h:panelGroup>
<h:panelGroup>
<p:password id="password" value="#{ChangePwdBean.password}"
styleClass="requested" redisplay="false" required="true"
tabindex="2"
validator="#{ChangePwdBean.validatePassword}" />
<p:message for="password"/>
</h:panelGroup>
<h:panelGroup>
<p:password id="passwordVerify" styleClass="requested"
redisplay="false" required="true" tabindex="3"
value="#{ChangePwdBean.passwordVerify}"
validator="#{ChangePwdBean.validatePasswordVerify}" />
<p:message for="passwordVerify"/>
</h:panelGroup>
</h:panelGrid>
</h:panelGroup>
<h:panelGroup layout="block" styleClass="divLoginButton">
<p:commandButton id="btnLogin" action="#{ChangePwdBean.changePwd}"
value="#{appl['buttons.change.password']}"
styleClass="buttonChangePwd" tabindex="4" ajax="false"
style="margin-right: 3px;"
update="@form">
</p:commandButton>
<p:commandButton id="btnLogout" value="#{appl['buttons.cancel']}"
action="toLogout" immediate="true" styleClass="buttonCancel" />
</h:panelGroup>
<p:focus for="oldpassword" />
</h:form>
</ui:define>
</ui:composition>
后端bean中向页面添加消息的代码如下:
public static void addErrorMessage(final String clientId, final String message, final Object[] param) {
final UIComponent uiComp = setComponentToInvalid(clientId);
FacesContext.getCurrentInstance().addMessage(uiComp != null ? uiComp.getClientId() : null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, StringUtils.EMPTY));
}
protected static UIComponent setComponentToInvalid(final String clientId) {
final UIComponent uiComponent = getUIComponent(null, clientId);
if (uiComponent != null && uiComponent instanceof UIInput) {
((UIInput) uiComponent).setValid(false);
}
return uiComponent;
}
并对此方法进行示例调用:
addErrorMessage("password", "Password is too short!", new Object[] {});
在此先感谢您的帮助!