5
我在JSF上創建了一個簡單的示例。它是小型的登錄應用程序。我的問題是我的網頁上重複的錯誤消息:頁面上的錯誤消息JSF中的<h:messages>
我有兩個h:message
(用戶名和密碼)標籤和一個h:messages
我的網頁上(全局消息):
<h:form id="loginForm">
<h:panelGrid columns="3">
<h:outputText value="Username" />
<h:inputText id="username" value="#{loginBean.username}"
required="true" requiredMessage="Please enter your username" />
<h:message for="username" errorClass="errorMessage" /> <!-- Message for username -->
<h:outputText value="Password" />
<h:inputSecret id="password" value="#{loginBean.password}"
required="true" requiredMessage="Please enter your password" />
<h:message for="password" errorClass="errorMessage" /> <!-- Message for password -->
<h:commandButton value="Login" action="#{loginBean.login}" />
</h:panelGrid>
<h:messages styleClass="errorMessage" /> <!-- Global messages -->
</h:form>
操作方法login()
我支持bean中:
public String login() {
FacesContext facesContext = FacesContext.getCurrentInstance();
if ("admin".equals(username) && "pass".equals(password)) {
return "success";
} else {
facesContext.addMessage("loginForm", new FacesMessage("Username or password is incorrect"));
return null;
}
}
我想顯示關於僅在這些字段旁邊的空用戶名或密碼的消息,不在全局消息中的我的按鈕下。如果密碼或用戶名不正確,我想在全局消息中輸入我的操作方法中的錯誤消息。我該如何解決這個問題?
我試圖用屬性globalOnly
:
<h:messages styleClass="errorMessage" globalOnly="true" /> <!-- Global messages -->
,但它並沒有完全解決我的問題,因爲全球的消息不會在這種情況下顯示在所有。
在此先感謝!
非常感謝!有用! – Michael