使用 JavaScript 向 JSF 发送 Ajax 请求

发布于 2024-11-11 18:16:42 字数 1307 浏览 3 评论 0原文

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

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

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

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

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

还有一点,当戴夫盆栽时,这些豆子对我不起作用。我必须将其更改如下: @ManagedBean(名称 = "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 我会收到错误!

Currently, I am working on a web application that uses draw2d library to draw shapes and charts. On the server side I am using Java(JSF).

I can say that 95% of this application is just pure JavaScript. I like to be able to send Ajax requests from inside my JavaScript with no need of using hidden fields such as <f:inputText> (perhaps using jQuery Ajax components?).

I tried to hack around this by adding different hidden JFS component and jsf.ajax.request, but for whatever reason they are not very reliable and sometimes they don't send the ajax request.

Any suggestion? Also, about jQuery, I am not sure how to process the request on the server side.

Answer: I ended up using what Dave suggested. I previously tried to use jsFunction from this link, but I got error. Apparently, the problem is , which is not yet implemented in Richfaces 4. However, if you use , as dave mentioned it works perfectly.

One more point, the beans didn't work for me as Dave potsed. I had to change it as follows:
@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;
}
}

I am not sure why this is the case, but if I remove getActionMethod or actionMenthod I would get error!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千年*琉璃梦 2024-11-18 18:16:42

如果您想要使用第三方库 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;
    }


}

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


}
冷清清 2024-11-18 18:16:42

不幸的是,JSF JS API 不支持这一点。不过,此增强功能是作为 JSF 规范问题 613 请求的。请随意投票。

到目前为止,您最好的选择实际上是拥有一个带有 的不可见表单,该表单由编程提交触发。

例如,

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

带有 jQ​​uery

<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() {
    // ...
}

// ...

Facelet ###另请参阅:


更新:现在,5年后,我个人使用新的 JSF 组件 实现了规范问题 613。这主要基于 OmniFaces 。这将在即将发布的 JSF 2.3 中提供,目前已经作为一个里程碑推出。 2.3.0-m06

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.

素年丶 2024-11-18 18:16:42

我意识到这已经很旧了,但它仍然出现在有关该主题的谷歌搜索中。

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文