h:带有 for 属性的消息元素不会在 JSF 2 中显示消息
我在 Glassfish 3 上运行的 JSF 中有以下身份验证表单:
<h:messages globalOnly="true" errorClass="erro" />
<h:form id="form-login">
<h:outputText value="User" />
<h:message for="login-user" />
<h:inputText id="login-user"
value="#{authenticationBean.user}" showMessages="true" />
<h:outputText value="Password" />
<h:inputSecret id="login-password"
value="#{authenticationBean.password}" />
<h:commandButton id="Log in"
action="#{authenticationBean.logIn}" value="Autenticar-se" />
</h:form>
authentication.logIn
方法如下:
public String logIn() {
user = userDAO.authenticate(user, password);
if (user != null) {
FacesMessage message = new FacesMessage("Welcome!");
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, message);
return "ok";
} else {
FacesMessage message = new FacesMessage("Authentication failed");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, message);
if ("".equals(user)) {
message = new FacesMessage("You need to give a user");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(
"login-user", message);
}
return "fail";
}
}
如果未给出用户登录名,我会尝试显示一条特定消息,并使用行
。但它不起作用:如果我单击带有空用户字段的提交按钮,则
元素会显示 < code>"身份验证失败" 但
组件只是不显示 "You need to go a user “
文本如我所料。相反,它根本不显示任何消息。另外,日志中会出现以下警告:(
[lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=login-user[severity=(ERROR 2), summary=(You need to give a user), detail=(You need to give a user)]
实际上,我在 Google 中查找了一些翻译,因为我收到的真实消息是葡萄牙语的:)
INFO: WARN: FacesMessage(s) foram enfileirados, mas podem não ter sido exibidos.
sourceId=login-usuario[severity=(ERROR 2), summary=(You need to give a user), detail=(You need to give a user)]
我
做错了什么?
提前致谢!
I have the following authentication form in JSF running on Glassfish 3:
<h:messages globalOnly="true" errorClass="erro" />
<h:form id="form-login">
<h:outputText value="User" />
<h:message for="login-user" />
<h:inputText id="login-user"
value="#{authenticationBean.user}" showMessages="true" />
<h:outputText value="Password" />
<h:inputSecret id="login-password"
value="#{authenticationBean.password}" />
<h:commandButton id="Log in"
action="#{authenticationBean.logIn}" value="Autenticar-se" />
</h:form>
The authentication.logIn
method is the one below:
public String logIn() {
user = userDAO.authenticate(user, password);
if (user != null) {
FacesMessage message = new FacesMessage("Welcome!");
message.setSeverity(FacesMessage.SEVERITY_INFO);
FacesContext.getCurrentInstance().addMessage(null, message);
return "ok";
} else {
FacesMessage message = new FacesMessage("Authentication failed");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(null, message);
if ("".equals(user)) {
message = new FacesMessage("You need to give a user");
message.setSeverity(FacesMessage.SEVERITY_ERROR);
FacesContext.getCurrentInstance().addMessage(
"login-user", message);
}
return "fail";
}
}
I try to show an specific message if the user login is not given, with the line <h:message for="login-user" />
. It does not work, though: if I click in the submit button with an empty user field, the <h:messages globalOnly="true" errorClass="erro" />
element presents the "Authentication failed"
but the <h:message for="login-user" />
component just does not present the "You need to give a user"
text as I expected. Instead it presents no message at all. Also, the following warning appears on the log:
[lifecycle] WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=login-user[severity=(ERROR 2), summary=(You need to give a user), detail=(You need to give a user)]
(Actually, I looked for some translation in Google, since the real message I got is in Portuguese:
INFO: WARN: FacesMessage(s) foram enfileirados, mas podem não ter sido exibidos.
sourceId=login-usuario[severity=(ERROR 2), summary=(You need to give a user), detail=(You need to give a user)]
)
What am I doing wrong?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定客户端 ID,而不是组件 ID。根据给定的视图代码,这将是:
如果这不起作用,您需要在浏览器中打开页面并查看其生成的 HTML 源代码。它应该与生成的 HTML
元素的
id
属性相同。与问题无关,我通常使用
null
作为客户端 ID,并使用
来处理一般验证错误。您可能想采取相同的方法。对于“您需要提供用户”部分,我只需使用required="true"
即可。You need to specify the client ID, not the component ID. Based on the given view code, that'll be:
If that doesn't work, you need to open the page in browser and view its generated HTML source. It should be the same as
id
attribute of generated HTML<input>
element.Unrelated to the problem, I usually use
null
as client ID and use a<h:messages globalOnly="true">
for general validation errors. You may want to take the same approach. For the "You need to give an user" part I'd just userequired="true"
instead.