4

我正在使用 JSF 来构建一个站点。我的主页上包含了jQuery Gritter (Growl)通知。是否可以在before_close:of$.gritter.add函数中调用托管 bean 方法?

我要使用的代码如下:

<h:body>
    <c:forEach items="#{notificationBean.growlNotificationList}" var="p">
        <script>
        /* <![CDATA[ */
        $.gritter.add({
            // (string | mandatory) the heading of the notification
            title: 'Notification',
            // (string | mandatory) the text inside the notification
            text: 'Comment on your staus',
            // (bool | optional) if you want it to fade out on its own or just sit there
            sticky: true, 
            // (int | optional) the time you want it to be alive for before fading out (milliseconds)
            time: 8000,
            // (string | optional) the class name you want to apply directly to the notification for custom styling
            class_name: 'gritter-light',
            // (function | optional) function called before it closes
            before_close: function(e, manual_close){
                '#{notificationBean.set0ToGrowlToShow(p.notificationID)}'
            }
        });
        /* ]]> */
        </script>
    </c:forEach>
</h:body>
4

2 回答 2

5

您当前的尝试仅将给定的 EL 表达式解释为值表达式,并在生成嵌入了 JS 代码的 HTML 输出期间立即打印其结果。就像您正在使用<h:outputText>. 这确实行不通。

然而,功能要求是可以理解的。标准的 JSF API 没有为此提供现成的解决方案。如果您想坚持使用标准的 JSF API,最好的办法是创建一个带有隐藏命令链接的隐藏表单,您可以使用 JavaScript 触发该链接。

基本上,

<h:form id="form" style="display:none">
    <h:inputHidden id="id" value="#{notificationBean.notificationID}" />
    <h:commandLink id="command" action="#{notificationBean.set0ToGrowlToShow}">
        <f:ajax execute="@form" />
    </h:commandLink>
</h:form>

$("[id='form:id']").val(#{p.notificationID});    
$("[id='form:command']").click();

但是,这很笨拙。考虑寻找第 3 方 JSF 组件甚至实用程序库来满足要求。JSF 实用程序库OmniFaces具有<o:commandScript>用于此目的的组件。另请参阅其展示页面

<h:form>
    <o:commandScript name="set0ToGrowlToShow" action="#{notificationBean.set0ToGrowlToShow}" />
</h:form>

set0ToGrowlToShow(#{p.notificationID});

(请注意,这是设置为 HTTP 请求参数,而不是操作方法参数)

JSF 组件库PrimeFaces具有<p:remoteCommand><o:commandScript>. 另请参阅其展示页面。更重要的是,PrimeFaces 有一个即用型<p:growl>组件,其功能与您的 jQuery 插件基本相同!另请参阅其展示页面。而不是你的整个 jQuery 事情,你可以这样做:

<p:growl globalOnly="true" autoUpdate="true" />

并通过以下方式向其提供消息

facesContext.addMessage(null, message);

也可以看看:

于 2013-03-27T20:06:02.933 回答
0

您需要了解您需要调用在服务器中运行的 java 方法,并且您不能直接调用它。

在您的情况下,我建议使用 AJAX 或在页面加载时读取值并使用它(如果对您可行)

检查如何将AJAX 与 Jquery 一起使用

于 2013-03-27T19:59:05.033 回答