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.
我正在寻找一种简单的方法来调用bean的方法,该方法不带参数并在JSF中返回一个字符串。我真的不需要的是该方法返回一个动作结果,然后使用整个JSF生命周期来获取另一个视图。我需要从JavaScript中做到这一点,以便我可以将应用程序的一些客户端部分组合在一起并且浏览RichFaces的A4J部分到目前为止没有给我带来任何帮助。
So here's the scenario again in a step-by-step form:
所以这里的场景又是一步一步的形式:
- from JS issue a GET on some address
- on the server process that GET and return JSON or HTML (basically a string)
- once the request is sent back to the client I want to be able to process it further with JS.
来自JS在某些地址发出GET
在GET上返回JSON或HTML的服务器进程(基本上是一个字符串)
一旦请求被发送回客户端,我希望能够使用JS进一步处理它。
Thanks!
2 个解决方案
#1
Use a4j:jsFunction and the data attribute.
使用a4j:jsFunction和data属性。
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>
#2
Damo: can you explain why it might only work for the first time the method callBackend is executed? I'm experiencing a strange behavior that the first call succeeds and the next calls are just blocked. I see the server-side code being executed but some strange result is being sent back to the browser (something like the _viewstate and those kind of things).
达摩:你能解释为什么它只能在第一次执行方法callBackend时工作吗?我遇到了一个奇怪的行为,第一个呼叫成功,下一个呼叫被阻止。我看到服务器端代码正在执行但是一些奇怪的结果被发送回浏览器(类似于_viewstate和那些东西)。
#1
Use a4j:jsFunction and the data attribute.
使用a4j:jsFunction和data属性。
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>
#2
Damo: can you explain why it might only work for the first time the method callBackend is executed? I'm experiencing a strange behavior that the first call succeeds and the next calls are just blocked. I see the server-side code being executed but some strange result is being sent back to the browser (something like the _viewstate and those kind of things).
达摩:你能解释为什么它只能在第一次执行方法callBackend时工作吗?我遇到了一个奇怪的行为,第一个呼叫成功,下一个呼叫被阻止。我看到服务器端代码正在执行但是一些奇怪的结果被发送回浏览器(类似于_viewstate和那些东西)。