目前,我正在开发一个使用draw2d库绘制形状和图表的Web应用程序。在服务器端我使用Java(JSF)。
我可以说这个应用程序的95%只是纯JavaScript。我希望能够从我的JavaScript中发送Ajax请求,而不需要使用<f:inputText>
等隐藏字段(可能使用jQuery Ajax组件?)。
我尝试通过添加不同的隐藏JFS组件和jsf.ajax.request
来解决这个问题,但无论出于何种原因,它们都不是非常可靠,有时它们不会发送ajax请求。
有什么建议吗?另外,关于jQuery,我不知道如何在服务器端处理请求。
答案:我最终使用了戴夫的建议。我之前尝试使用此link中的jsFunction,但是我收到了错误。显然,问题是,在Richfaces 4中尚未实现。但是,如果你使用,正如戴夫所提到的那样它可以很好地工作。
还有一点,当Dave喝醉时,豆子对我不起作用。我不得不改变如下: @ManagedBean(name =“jsFunctionBean”) @SessionScoped 公共类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,我会收到错误!
答案 0 :(得分:5)
如果您要使用第三方库Richfaces a4j:jsFunction提供了使用javascript函数调用服务器端方法的功能,以及将序列化为json的对象传递回回调函数的能力:
<h:form id="form1" prependId="false">
<a4j:jsFunction
name="submitApplication"
action="#{jsFunctionBean.actionMethod}"
data="#{jsFunctionBean}"
oncomplete="processData(event.data)"
immediate="true">
</a4j:jsFunction>
<script type="text/javascript">
//example callback function
function processData(data) {
alert(data.test);
alert(data.name);
}
//call the ajax method from javascript
submitApplication();
</script>
</h:form>
你的豆子:
@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;
}
}
答案 1 :(得分:3)
不幸的是,JSF JS API不支持这一点。但是,此增强功能需要JSF spec issue 613。随意投票。
到目前为止,您最好的选择是拥有一个<f:ajax>
的隐形表单,该表单由程序化提交触发。
例如,这个Facelet
<h:form id="form" style="display:none;">
<h:inputText id="input1" value="#{bean.input1}" />
<h:inputText id="input2" value="#{bean.input2}" />
<h:inputText id="input3" value="#{bean.input3}" />
<h:commandButton value="submit" action="#{bean.submit}">
<f:ajax execute="@form" />
</h:commandButton>
</h:form>
使用这个jQuery
<script>
$('#form\\:input1').val('value1');
$('#form\\:input2').val('value2');
$('#form\\:input3').val('value3');
$('#form').submit();
</script>
和这个托管bean
private String input1;
private String input2;
private String input3;
public void submit() {
// ...
}
// ...
更新:现在,5年后,我个人implemented规范问题613带有新的JSF组件<h:commandScript>
。这主要基于OmniFaces <o:commandScript>
。这将在即将推出的JSF 2.3中提供,该版本目前已作为里程碑提供。自2.3.0-m06以来<h:commandScript>
可用。
答案 2 :(得分:0)
我意识到这已经过时了,但Google搜索仍会出现这个问题。
这里提供了一个很好的答案(概述了jsf.ajax客户端JS命名空间的最原始用法):How to use jsf.ajax.request to manually send ajax request in JSF。
此外,有关该主题的Oracle文档可在以下位置找到:http://docs.oracle.com/javaee/6/tutorial/doc/gkiow.html,其中详细介绍了使用JSF提供的f:ajax标记的AJAX与JSF的用法。