【问题标题】:How to customize the presentation of faces messages如何自定义人脸消息的呈现方式
【发布时间】:2012-11-23 01:47:49
【问题描述】:

我想自定义人脸消息的呈现方式。

为此,

<h:inputText id="name" required="true" />

验证失败时会显示在a中

<h:message for="name" />

但是,我想自定义演示调用JS如下:

<div class="notification"></div>
function showNotification(msg){
$(".notification").html(msg);
$(".notification").fadeIn(1000, function(){
    timeout = setTimeout(function(){
        $(".notification").fadeOut(1000);
    }, 5000);
});
}

我怎样才能做到这一点?

【问题讨论】:

    标签: validation jsf-2


    【解决方案1】:

    您可以使用FacesContext#getMessageList() 来获取视图中的消息,如果需要,可以使用特定客户端 ID 的消息。您可以在 ui:repeat 中迭代它们。每个项目都是一个FacesMessage,它有几个吸气剂。您可以使用 &lt;h:outputText escape="false"&gt; 在消息中显示任何未转义的 HTML。

    所以,简而言之:

    <ui:repeat value="#{facesContext.messageList('form:name')}" var="message">
        <div><h:outputText value="#{message.summary}" escape="false" /></div>
    </ui:repeat>
    

    (在上面的例子中,我假设你的表单有id="form"

    或者,如果该 HTML 帮助链接实际上不是消息的一部分,那么:

    <ui:repeat value="#{facesContext.messageList('form:name')}" var="message">
        <div>#{message.summary} <a href="help.xhtml">help</a></div>
    </ui:repeat>
    

    【讨论】:

    • 只要让 JSF 打印 &lt;script&gt;showNotification('#{message.summary}')&lt;/script&gt;,前提是您的消息不包含可能破坏 JS 代码的字符,例如换行符。
    猜你喜欢
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-29
    • 2021-06-23
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多