javascript - Calling JSF managed bean method with arguments in jQuery - Stack Overflow

I am using JSF to build a site. I have included jQuery Gritter (Growl) notification on my home page. Is

I am using JSF to build a site. I have included jQuery Gritter (Growl) notification on my home page. Is it possible to call a managed bean method inside the before_close: of $.gritter.add function?

The code that I want to use is as follows:

<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>

I am using JSF to build a site. I have included jQuery Gritter (Growl) notification on my home page. Is it possible to call a managed bean method inside the before_close: of $.gritter.add function?

The code that I want to use is as follows:

<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>
Share Improve this question edited Dec 21, 2016 at 18:29 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Mar 27, 2013 at 19:50 user2129151user2129151
Add a ment  | 

2 Answers 2

Reset to default 5

Your current attempt merely interprets the given EL expression as a value expression and just prints its result immediately during producing the HTML output with the JS code embedded. It's like as if you're using <h:outputText>. This is indeed not going to work.

The functional requirement is however understood. The standard JSF API does not offer a ready-to-use solution for this. If you want to stick to standard JSF API, your best bet is to create a hidden form with a hidden mand link which you trigger using JavaScript.

Basically,

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

with

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

However, this is pretty clumsy. Consider looking for a 3rd party JSF ponent or even utility library to achieve the requirement anyway. The JSF utility library OmniFaces has the <o:mandScript> ponent for this. See also its showcase page.

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

with

set0ToGrowlToShow(#{p.notificationID});

(please note that this is set as HTTP request parameter, not as action method argument)

The JSF ponent library PrimeFaces has the <p:remoteCommand> for this which is much similar to <o:mandScript>. See also its showcase page. Even more, PrimeFaces has a ready-to-use <p:growl> ponent which does essentially the same as your jQuery plugin! See also its showcase page. Instead of your whole jQuery thing you can just do:

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

and feed it with messages by

facesContext.addMessage(null, message);

See also:

  • How to invoke a JSF managed bean on a HTML DOM event using native JavaScript?
  • How to pass JavaScript variables as parameters to JSF action method?

You need to understand that you need to call a java method that runs in the server and you CANNOT call it directly.

In your case, I would remend to use AJAX or have the value read on load of the page and use it (if feasible for you)

Check how you can use AJAX with Jquery

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744180050a4561937.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信