我已经阅读了很多如何添加FacesMessage的指南,但没有任何工作,我不明白为什么?
我总是得到以下警告,但我现在不知道如何修复它?
[Warnung ] There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered.
These unhandled FacesMessages are:
- Login failed
My JSF View:
<!-- Login Form -->
<h:form id="loginForm" role="form" style="width: 400px; margin:0 auto;">
<div class="required-field-block">
<h:outputLabel for="email" value="Email-Adresse"></h:outputLabel>
<h:inputText id="email" label="Email" value="#{loginBean.email}" class="form-control" required="true" requiredMessage="Keine Mailadresse angeben"></h:inputText>
<h:message for="email" class="text text-danger"></h:message>
</div>
<div class="required-field-block">
<h:outputLabel for="password" value="Passwort"></h:outputLabel>
<h:inputText id="password" label="Passwort" value="#{loginBean.password}" class="form-control" required="true" requiredMessage="Kein Passwort angegeben"></h:inputText>
<h:message for="password" class="text text-danger"></h:message>
</div>
<h:commandButton id="sendLogin" update="loginForm" value="Login" type="submit" actionListener="#{loginBean.login}" class="btn btn-primary"></h:commandButton>
<h:message globalOnly="true"></h:message>
</h:form>
和我的代码从bean:
public String login() {
UserManagement man = new UserManagement();
try {
user = man.getUserByEmail(this.email);
} catch (SQLException | IndexOutOfBoundsException e) {
String msg = "Login failed";
addErrorMessage(msg);
return "loginFailed";
}
if(this.user!=null&&this.user.getPassword().equals(password)) {
this.loggedIn = true;
return "loginSucceed";
}
else {
String msg = "Unvalid Password";
addErrorMessage(msg);
return "loginFailed";
}
}
private void addErrorMessage(String msg) {
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage fm = new FacesMessage(msg);
context.addMessage(null, fm);
}
我做错了什么?为什么这条信息没有呈现出来?我有点绝望,因为我尝试了所有的ID,但没有任何效果,我很困惑为什么它不起作用。也许这只是一个愚蠢的错误,我一直没有看到:(
h:message
组件只呈现来自您必须使用for
属性引用的组件的单个消息。
如果你想渲染所有的消息,只需使用h:messages
代替:
<h:messages/>