从JavaScript调用JSF中的服务器端代码 [英] Call from JavaScript to server-side code in JSF

查看:68
本文介绍了从JavaScript调用JSF中的服务器端代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种简单的方法来调用bean的方法,该方法将不带任何参数并在JSF中返回一个字符串.我真正不需要的是该方法返回一个动作结果,然后使用整个JSF生命周期来使我进入另一个视图.我需要使用JavaScript进行此操作,以便可以将应用程序的某些客户端部分放在一起,并且遍历RichFaces的A4J部分至今还没有给我带来什么好处.

I'm looking for an easy way to call a bean's method that will take no parameters and return a string in JSF. The thing that I don't really need is that the method returns an action result and then uses the whole JSF life-cycle to do get me to another view. I need to do that from JavaScript so that I can put together some client-side parts of the application and going over the A4J part of RichFaces has brought me nothing so far.

所以这是分步形式的场景:

So here's the scenario again in a step-by-step form:

  1. 从JS发出一些地址上的GET
  2. 在获取和返回JSON或HTML(基本上是字符串)的服务器进程上
  3. 将请求发送回客户端后,我希望能够使用JS对其进行进一步处理.

谢谢!

推荐答案

使用 a4j:jsFunction 和data属性.

Use a4j:jsFunction and the data attribute.

因此,您大致需要这样的东西:

So roughly you want something like:

<button onclick="callBackend();">Go</button>

<a4j:jsFunction name="callBackend" action="#{myBean.someMethod}" data="#{myBean.someString}" oncomplete="handleResponse(data);"/>

<script>
function handleResponse(response) {
   alert(response);
}
</script>

这篇关于从JavaScript调用JSF中的服务器端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆