【问题标题】:JSF FacesMessage shows detail message instead of summary messageJSF FacesMessage 显示详细消息而不是摘要消息
【发布时间】:2017-07-15 14:22:15
【问题描述】:

我不清楚addMessage()FacesMessage 是如何工作的。如果我没有指定<h:message> 来显示消息,则会显示摘要消息。但是如果我为其创建一个<h:message> 字段,则会显示详细消息。为什么?如何选择要显示的消息?以下是我的代码:

豆子

public String login()
{
    if(password.equals("123123"))
    {
        session.setAttribute("username", username);
        return "home";
    }
    else
    {
        FacesContext.getCurrentInstance().addMessage("loginForm", new FacesMessage(FacesMessage.SEVERITY_WARN,"Incorrect Username and Passowrd", "Please enter correct username and Password"));
        return "login";
    }
}

JSF

<h:form id="loginForm">
    <h:outputLabel for="username" value="Username: " />
    <h:inputText id="username" value="#{loginBean.username}" required="true" requiredMessage="Username is required" />
    <h:message for="username"></h:message>
    <br /><br />

    <h:outputLabel for="password" value="Password: " />
    <h:inputSecret id="password" value="#{loginBean.password}"></h:inputSecret>
    <h:message for="password"></h:message>
    <br /><br />

    <h:commandButton action="#{loginBean.login()}" value="Login"></h:commandButton>
    <br /><br />
</h:form>

上面的代码将产生以下输出:

但如果我将&lt;h:message for="loginForm"&gt;&lt;/h:message&gt; 添加到我的代码中,输出将是:

它显示的是详细消息而不是摘要消息,实际上这没什么大不了的,但我只想知道为什么会这样?谁能解释一下?

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    当您将javax.faces.PROJECT_STAGE 设置为Development 并且您忘记在视图中声明&lt;h:messages&gt; 时,该橙色消息将出现。在这种情况下,JSF 无法在页面的任何位置显示消息。通常,这只会在服务器日志中记录为警告,如下所示:

    There are some unhandled FacesMessages, this means not every FacesMessage had a chance to be rendered

    但是,为了方便开发者,当项目阶段设置为开发时,它也会附加到网页的末尾。但是你不应该依赖它作为 webapp 的正常功能。您应该将其视为您忽略了某些事情或做错了什么的暗示。正确的解决方案是添加一个适当的&lt;h:message for="xxx"&gt; 或至少一个&lt;h:messages&gt; 作为后备。

    开发阶段消息显示摘要而不是详细信息是您最不关心的问题。首先它应该根本没有显示出来。

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2012-08-19
      • 1970-01-01
      • 1970-01-01
      • 2019-11-05
      • 1970-01-01
      相关资源
      最近更新 更多