gpt4 book ai didi

jquery - 使用 JavaScript 向 JSF 发送 Ajax 请求

转载 作者:行者123 更新时间:2023-11-30 23:56:23 24 4
gpt4 key购买 nike

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

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

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

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

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

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

最佳答案

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


}

关于jquery - 使用 JavaScript 向 JSF 发送 Ajax 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6188981/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com