使用JavaScript向Ajf发送Ajax请求

Rez*_*eza 3 ajax jquery jsf draw2d

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

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

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

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

答:我最终使用了戴夫的建议.我之前尝试过使用此链接中的 jsFunction ,但是我收到了错误.显然,问题是,在Richfaces 4中尚未实现.但是,如果你使用,正如戴夫提到的那样它可以很好地工作.

还有一点,豆子对我来说不起作用,因为戴夫喝了.我必须按如下方式更改它:@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;
}
}
Run Code Online (Sandbox Code Playgroud)

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

Bal*_*usC 5

不幸的是,JSF JS API 不支持这一点。然而,这种增强是作为JSF 规范问题 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>
Run Code Online (Sandbox Code Playgroud)

使用这个 jQuery

<script>
    $('#form\\:input1').val('value1');
    $('#form\\:input2').val('value2');
    $('#form\\:input3').val('value3');
    $('#form').submit();
</script>
Run Code Online (Sandbox Code Playgroud)

和这个托管 bean

private String input1;
private String input2;
private String input3;

public void submit() {
    // ...
}

// ...
Run Code Online (Sandbox Code Playgroud)

###也可以看看:


更新:现在,5年后,我曾亲自实施规范问题613与新的JSF组件的<h:commandScript>。这主要基于OmniFaces<o:commandScript>。这将在即将推出的 JSF 2.3 中可用,该版本目前已作为里程碑可用。该<h:commandScript>是因为可用的2.3.0-M06


Dav*_*ple 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>
Run Code Online (Sandbox Code Playgroud)

还有你的豆子:

@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;
    }


}
Run Code Online (Sandbox Code Playgroud)