使用JavaScript发送Ajax请求到JSF(Sending Ajax Request Using JavaScript to JSF)

编程入门 行业动态 更新时间:2024-10-21 16:19:29
使用JavaScript发送Ajax请求到JSF(Sending Ajax Request Using JavaScript to JSF)

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

我可以说,这个应用程序的95%是纯粹的JavaScript。 我喜欢能够从JavaScript内部发送Ajax请求,而不需要使用诸如<f:inputText>类的隐藏字段(可能使用jQuery Ajax组件?)。

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

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

答:我最终使用了戴夫的建议。 我以前试图从这个链接使用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,我会得到错误!

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!

最满意答案

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

和你的豆子:

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

更多推荐

使用,Dave,public,jQuery,电脑培训,计算机培训,IT培训"/> <meta name="descrip

本文发布于:2023-08-07 21:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465581.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Ajax   JavaScript   JSF   Request   Sending

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!