使用 jquery ajax 调用 jsf 托管 bean 方法(AjaxBehaviorEvent 侦听器处理程序)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14475969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 13:36:52  来源:igfitidea点击:

Using jquery ajax to call a jsf managed bean method (an AjaxBehaviorEvent listener handler)

jqueryajaxjsfmanaged-bean

提问by Bardelman

i would like to know if there is a way to fire a jsf managed bean method (with an AjaxBehaviorEvent type parameter: the same triggered when using f:ajax) directly by using a jquery ajax server request.By the way , i m a jsf developper and i didn't find an example about using jquery ajax with Java EE as a server-side framework, all examples i found were with php..so i wish to get a complete example about doing that. i think the other workaround maybe is to make a commandLink being submitted with jquery on the client side and passing parameters through that call but i prefer the former solution and i wish it work.

我想知道是否有办法直接通过使用 jquery ajax 服务器请求来触发 jsf 托管 bean 方法(使用 AjaxBehaviorEvent 类型参数:使用 f:ajax 时触发相同)。顺便说一句,ima jsf 开发人员和我没有找到关于将 jquery ajax 与 Java EE 一起用作服务器端框架的示例,我找到的所有示例都使用 php..所以我希望得到一个关于这样做的完整示例。我认为另一种解决方法可能是在客户端使用 jquery 提交 commandLink 并通过该调用传递参数,但我更喜欢前一种解决方案,我希望它起作用。

Thanks very much for help !

非常感谢您的帮助!

回答by islandguy

Here you go:

干得好:

<script type="text/javascript">

    doAwesomeness();

</script>

In your page:

在您的页面中:

<a4j:jsFunction name="doAwesomeness" action="#{awesomeBean.awesomeMethod}"/>

Good luck!

祝你好运!

回答by Daniel

jQuery ajax request and JSF ajax request uses different js library's , I don't think there is a point in trying to mix those to too much...

jQuery ajax 请求和 JSF ajax 请求使用不同的 js 库,我认为尝试将它们混合太多没有意义......

If you whish to fire a JSF managed bean action from jQuery you better use a hidden h:commandButtonfor that purpose...

如果您希望从 jQuery 触发 JSF 托管 bean 操作,您最好h:commandButton为此目的使用 hidden ...

JSF:

JSF:

<h:commandButton id="someId" action="#{someBean.someMethod}" style="display:none">
    <f:ajax render="someId" execute="someId"/>
</h:commandButton>

if you want to pass some more hidden args you can add some more hidden JSF components ids in the hidden h:commandButtonexecuteattribute, that way their corresponding properties will be updated on server side...

如果您想传递更多隐藏的参数,您可以在隐藏h:commandButtonexecute属性中添加更多隐藏的 JSF 组件 ID ,这样它们的相应属性将在服务器端更新...

js

js

$("#someId").click();


On the other side , if you want to use managed bean data in servlets that corresponds to your jQuery calls you can always access that JSF managed data, like this : JSF - get managed bean by name

另一方面,如果您想在对应于您的 jQuery 调用的 servlet 中使用托管 bean 数据,您始终可以访问该 JSF 托管数据,如下所示:JSF - 按名称获取托管 bean

回答by Pierre C

In the same vein as pointed out by islandguy, if you use Primefaces, you woud use the <p:remoteCommand/>command, as follows :

与 islandguy 指出的一样,如果您使用 Primefaces,您将使用以下<p:remoteCommand/>命令:

<script type="text/javascript">
      doAwesomeness();
</script>

with :

和 :

 <p:remoteCommand name="doAwesomeness" 
          actionListener="#{awesomeBean.awesomeMethod}"
          oncomplete="jsToExecuteOnAjaxSuccess()" />

Hope this helps..

希望这可以帮助..