使用JavaScript向Ajf发送Ajax请求

目前,我正在开发一个使用draw2d库绘制形状和图表的Web应用程序。 在服务器端,我使用的是Java(JSF)。

我可以说这个应用程序的95%只是纯JavaScript。 我希望能够从我的JavaScript中发送Ajax请求,而不需要使用隐藏字段,如 (可能使用jQuery Ajax组件?)。

我试图通过添加不同的隐藏JFS组件和jsf.ajax.request来解决这个jsf.ajax.request ,但无论出于何种原因,它们都不是非常可靠,有时它们不会发送ajax请求。

有什么建议吗? 另外,关于jQuery,我不知道如何在服务器端处理请求。

答:我最终使用了戴夫的建议。 我之前尝试使用此链接中的 jsFunction,但是我收到了错误。 显然,问题是,在Richfaces 4中尚未实现。但是,如果你使用,正如戴夫所提到的那样它完美无缺。

还有一点,当Dave喝醉时,豆子对我不起作用。 我必须按如下方式更改它:@ManagedBean(name =“jsFunctionBean”)@SessionScoped public class JsFunctionBean {

 /** * Test String name */ private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } /** * Test boolean */ private boolean test; public boolean getTest() { return this.test; } public void setTest(boolean test) { this.test = test; } /** * Action Method * * @return */ public String getActionMethod() { return null; } public String actionMethod(){ this.test = true; this.name = "Dave"; return null; } } 

我不确定为什么会这样,但如果我删除了getActionMethod或actionMenthod,我会收到错误!

如果您要使用第三方库Richfaces a4j:jsFunction提供了使用javascript函数调用服务器端方法的function,以及将序列化为json的对象传递回回调函数的能力:

      

还有你的豆子:

 @ManagedBean(name = "jsFunctionBean") @SessionScoped public class JsFunctionBean { /** * Test String name */ private String name; public String getName() { return this.name; } public void setName(String name) { this.name = name; } /** * Test boolean */ private boolean test; public boolean getTest() { return this.test; } public void setTest(boolean test) { this.test = test; } /** * Action Method * * @return */ public String getActionMethod() { this.test = true; this.name = "Dave"; return null; } } 

不幸的是,JSF JS API不支持这一点。 然而,该增强作为JSF规范问题613被请求。 随意投票。

到目前为止,您最好的选择是使用一个由程序化提交触发的隐形表单。

例如,这个Facelet

  

用这个jQuery

  

和这个托管bean

 private String input1; private String input2; private String input3; public void submit() { // ... } // ... 

也可以看看:

  • 如何使用本机JavaScript在HTML DOM事件上调用JSF托管bean?
  • 如何使用jsf.ajax.request在JSF中手动发送ajax请求

更新 :现在,5年后,我亲自使用新的JSF组件 实现了规范问题613。 这主要基于OmniFaces 。 这将在即将推出的JSF 2.3中提供,该版本目前已作为里程碑提供。 自2.3.0-m06起可用。

我意识到这已经过时了,但Google搜索仍会出现这个主题。

这里提供了一个很好的答案(概述了jsf.ajax客户端JS命名空间的最原始用法): 如何使用jsf.ajax.request在JSF中手动发送ajax请求 。

此外,有关该主题的Oracle文档可以在以下url找到: http : //docs.oracle.com/javaee/6/tutorial/doc/gkiow.html ,其中详细介绍了使用JSF提供的f:ajax标记使用AJF和JSF的用法。