5

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

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

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

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

答:我最终使用了 Dave 的建议。我以前尝试使用此链接中的 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 我会得到错误!

4

3 回答 3

6

如果您准备使用第三方库 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>

还有你的 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;
    }


}
于 2011-05-31T17:12:10.523 回答
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>

用这个 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 年后,我个人用一个新的 JSF 组件实现<h:commandScript>了规范问题 613 ,. 这主要基于OmniFaces<o:commandScript>。这将在即将推出的 JSF 2.3 中提供,目前已作为里程碑提供。从2.3.0-m06<h:commandScript>开始可用。

于 2011-05-31T16:38:13.637 回答
0

我意识到这是旧的,但它仍然出现在有关该主题的 Google 搜索中。

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

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

于 2016-06-13T23:08:59.257 回答