簡體   English   中英

使用jQuery中的參數調用JSF托管bean方法

[英]Calling JSF managed bean method with arguments in jQuery

我正在使用JSF來構建一個站點。 我在我的主頁上添加了jQuery Gritter(Growl)通知。 是否可以在$.gritter.add函數的before_close:中調用托管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>

您當前的嘗試只是將給定的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> ,與<o:commandScript>非常相似。 另請參見其展示頁面 更重要的是,PrimeFaces有一個隨時可用的<p:growl>組件,它與你的jQuery插件基本相同! 另請參見其展示頁面 你可以做的不是你的整個jQuery事情:

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

並通過消息提供它

facesContext.addMessage(null, message);

也可以看看:

您需要了解需要調用在服務器中運行的java方法,而不能直接調用它。

在您的情況下,我建議使用AJAX或在頁面加載時讀取值並使用它(如果可行的話)

檢查如何在Jquery中使用AJAX

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM