Calling JSF managed bean method with arguments in jQuery

后端 未结 2 459
-上瘾入骨i
-上瘾入骨i 2020-12-06 14:34

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_clo

2条回答
  •  星月不相逢
    2020-12-06 14:52

    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 . 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 command link which you trigger using JavaScript.

    Basically,

    
    

    with

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

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

    
        
    
    

    with

    set0ToGrowlToShow(#{p.notificationID});
    

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

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

    
    

    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?

提交回复
热议问题