使用 JSF AJAX 方法保持 JavaScript 模块范围

发布于 2024-12-02 20:35:08 字数 797 浏览 1 评论 0原文

我正在使用具有 JSF2 框架的应用程序。该框架的想法是管理后端(Java)和前端之间的连接。

我知道,我们使用的 JSF 版本在 DOM 中创建 jsf.ajax 对象,该对象包含多种用于执行传递和获取数据的方法。

因此,当我基于模块模式构建前端架构时,每个模块都有私有范围。在几个模块内,我需要进行 AJAX 调用以从后端获取一些数据,但是如果我通过调用 a4j:jsFunction (之前创建的)来使用 JSF 的标准方式,我就会失去 JavaScript 的范围立即模块。当然,也不能返回模块。

因此,我问是否可以通过使用 jsf.ajax 对象及其方法(请求/响应)来归档类似于 jQuery.ajax() 方法的内容?!

jQuery.ajax({
  url: "", // I don't have url as in PHP, I have JSF action listener #{method.function}
  data: { param: 'value' }, // params for method.function
  success: function(data){
    // yes I need to receive the data from Backend (maybe #{bean} or JSON)
  }
});

我坚信,有一种方法可以使用 JSF 和 AJAX for Java (A4J) 通过保留 JavaScript 模块的范围来满足我的需求,而在进行 AJAX 调用时无需超出该范围。

I am working with the application, which has JSF2 framework. The idea of that framework is to manage connection between Backend (Java) and Frontend.

I know, that JSF version we are using creates jsf.ajax object in a DOM, which holds several methods for performing passing and getting data.

And so, as I am building Frontend architecture based on modules pattern which has private scope each. Inside several modules I need to make AJAX call to get some data from Backend, but if I am using standart way of JSF by calling a4j:jsFunction (previously created), I am loosing the scope of my JavaScript module immediately. And, of course, can not return back to the module.

Therefore, I am asking is that possible to archive something similar to jQuery.ajax() method by using jsf.ajax object and its methods (request/reponse)?!

jQuery.ajax({
  url: "", // I don't have url as in PHP, I have JSF action listener #{method.function}
  data: { param: 'value' }, // params for method.function
  success: function(data){
    // yes I need to receive the data from Backend (maybe #{bean} or JSON)
  }
});

I strongly believe, there's a way to use JSF and AJAX for Java (A4J) to archive my need by keeping a scope of JavaScript module, without going out of that when making AJAX calls.

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

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

发布评论

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

评论(1

2024-12-09 20:35:08

如果您需要 ajax 组件来从服务器发布/检索任意数据,那么我认为 jsf.ajax 不是适合您的工具。它与 JSF 表示严格耦合,可用于调用服务器逻辑并呈现 html 页面的部分。响应将是带有部分更新的 XML。下面我将描述如何将其转向您的目的,但这不是此 api 的预期应用。

简而言之,必须这样调用:

jsf.ajax.request(<DOM_node>, event, {render: "<rendered_id>", execute: "@this"})

其中 DOM_node 是具有动作侦听器的元素,事件是侦听器支持的类型的 JS 事件(如果您使用 jsf.ajax.request 作为 onclick 那么只有“event”变量可用你可以按原样传递它)。;应该是您想要作为回调执行的脚本周围的 JSF 组件的客户端 ID。

结果调用应该要求 JSF 执行附加到与 DOM_node 相对应的组件(如 commandLink)的操作侦听器,并重新渲染包含在具有的组件内的区域。如果它包含 html

这是 a链接到文档

If you need an ajax component to post/retrieve arbitrary data from the server then I don't think jsf.ajax is the right tool for you. It is strictly coupled to the JSF presentation and can be used to invoke the server logic and render parts of the html page. The response will be an XML with partial-updates. Below I will describe how to bend it towards your purpose but it won't be intended application of this api.

In short this would have to be called like:

jsf.ajax.request(<DOM_node>, event, {render: "<rendered_id>", execute: "@this"})

Where DOM_node is element which has an action listener, event is JS event of type supported by the listener (if you use jsf.ajax.request as onclick then just "event" variable will be available and you can pass it as is). <rendered_id> should be client id of JSF component surrounding the script you want to execute as a callback.

The resulting call should ask JSF to execute action listener attached to component corresponding to DOM_node (like commandLink) and rerender area contained within component with <rendered_id>. If it contains html <script> tag, this script will be executed. It can contain your JSON data and function calls.

Here is a link to the docs.

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