Mic*_*ael 5 java jsf messages jsf-2
我在JSF上创建了一个简单的例子.这是一个小的登录应用程序.我的问题是我的页面上出现重复的错误消息:
我的页面上有两个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>
Run Code Online (Sandbox Code Playgroud)
login()
我的支持bean中的Action方法:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想在这些字段旁边显示关于空用户名或密码的消息,而不是在全局消息中的我的按钮下.如果密码或用户名不正确,我想在全局消息中从我的操作方法打印我的错误消息.我该如何解决这个问题?
我试图使用该属性globalOnly
:
<h:messages styleClass="errorMessage" globalOnly="true" /> <!-- Global messages -->
Run Code Online (Sandbox Code Playgroud)
但它并没有完全解决我的问题,因为在这种情况下根本不显示全局消息.
提前致谢!
Mat*_*ndy 17
的globalOnly
,如果你在你的支持bean正确地添加邮件交换器应该工作.client-id必须null
位于FacesContext.addMessage(...)
:
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(message));
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
37951 次 |
最近记录: |