私のprevious questionでは、ログインフォームからの検証メッセージを表示するという問題がありました。その問題は解決されましたが、今度はFacesContex#addMessage
というカスタムメッセージを表示できません。JSF FacesContext#addMessageが表示されない
JSF + PrimeFacesの使用。 LoginBean(SessionScoped
ManagedBean
)で
<p:dialog header="Login" widgetVar="loginDlg">
<h:form id="loginForm">
<h:panelGrid columns="3" cellpadding="5">
<h:outputLabel for="username" value="Username:" />
<p:inputText value="#{loginBean.username}" id="username" required="true" label="username" />
<p:message for="username" />
<h:outputLabel for="password" value="Password:" />
<h:inputSecret value="#{loginBean.password}" id="password" required="true" label="password" />
<p:message for="password" />
<f:facet name="footer">
<p:commandButton value="Login" id="loginDlgButton" update=":loginForm,:welcomeMsg" actionListener="#{loginBean.login}"
oncomplete="handleLoginRequest(xhr, status, args)"/>
<p:message for="loginDlgButton" />
</f:facet>
</h:panelGrid>
</h:form>
</p:dialog>
:
public void login() {
FacesContext context = FacesContext.getCurrentInstance();
RequestContext rContext = RequestContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
request.login(this.username, this.password);
rContext.addCallbackParam("loggedIn", true);
} catch (ServletException e) {
rContext.addCallbackParam("loggedIn", false);
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid credentials"));
}
}
このコード検証が成功し、ログインに失敗したとき、 "無効な資格" というメッセージを表示するが、ないはずです。さらに、私のウェブページの本文のどこかに次の行を追加しました:
<p:messages autoUpdate="true" />
私のメッセージはそこにも表示されません。
はのclientIdがnullの場合は、このFacesMessageのは、任意の特定のコンポーネントのインスタンス
に関連付けられていないと想定される。しかし、私はこれが何を意味するのか理解できないと言います。
ダニエルありがとう。他の答えに私のコメントを参照してください。 – perissf
@perissfこのアプローチはどうですか? http://stackoverflow.com/a/321722/617373 – Daniel
偉大なヒント、ありがとう – perissf