h:带有 for 属性的消息元素不会在 JSF 2 中显示消息

发布于 2024-10-17 20:00:02 字数 2397 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哀由 2024-10-24 20:00:02

您需要指定客户端 ID,而不是组件 ID。根据给定的视图代码,这将是:

FacesContext.getCurrentInstance().addMessage(
    "form-login:login-user", message);

如果这不起作用,您需要在浏览器中打开页面并查看其生成的 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:

FacesContext.getCurrentInstance().addMessage(
    "form-login:login-user", message);

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 use required="true" instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文