Sending Ajax Request Using JavaScript to JSF

谁都会走 提交于 2019-12-03 20:20:57

If you are up for a third party library Richfaces a4j:jsFunction offers the ability to invoke a server side method with a javascript function as well as the ability to pass a object serialized as json back to a callback function:

<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>

And your Bean:

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


}
BalusC

Unfortunately, the JSF JS API doesn't support this. This enhancement is however requested as JSF spec issue 613. Feel free to vote for it.

As far now, your best bet is really to have an invisible form with a <f:ajax> which is triggered by a programmatic submit.

For example, this 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>

with this jQuery

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

and this managed bean

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

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

// ...

See also:


Update: now, 5 years later, I have personally implemented spec issue 613 with a new JSF component, the <h:commandScript>. This is largely based on OmniFaces <o:commandScript>. This will be available in upcoming JSF 2.3, which is currently already available as a milestone. The <h:commandScript> is available since 2.3.0-m06.

SwampDev

I realize this is old, but it still comes up in Google searches regarding the subject.

A great answer has been provided here (which outlines the most raw usage of the jsf.ajax client side JS namespace): How to use jsf.ajax.request to manually send ajax request in JSF.

Also, Oracle documentation on the subject can be found at: http://docs.oracle.com/javaee/6/tutorial/doc/gkiow.html, which details the usage of AJAX with JSF using JSF's provided f:ajax tag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!