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();

ただし、これはかなり厄介です。とにかく要件を達成するために、サードパーティの JSF コンポーネントまたはユーティリティ ライブラリを探すことを検討してください。JSF ユーティリティ ライブラリOmniFacesには、<o:commandScript>このためのコンポーネントがあります。ショーケース ページも参照してください。

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

set0ToGrowlToShow(#{p.notificationID});

(これはアクション メソッドの引数としてではなく、HTTP リクエスト パラメータとして設定されることに注意してください)

JSF コンポーネント ライブラリPrimeFacesには、<p:remoteCommand>for this によく似た があり<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を使用するか、ページの読み込み時に値を読み取って使用することをお勧めします(可能な場合)

JqueryでAJAXを使用する方法を確認してください

于 2013-03-27T19:59:05.033 に答える