views:

554

answers:

2

Hi, 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. from JS issue a GET on some address
  2. on the server process that GET and return JSON or HTML (basically a string)
  3. once the request is sent back to the client I want to be able to process it further with JS.

Thanks!

+1  A: 

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>
Damo
Roughly yes, but I'd prefer the result be returned by someMethod rather than by myBean.getSomeString().
Matthias Hryniszak
I haven't tried it but you could drop the action and put the someMethod() in the data?
Damo
I did that. it's just that the data method must be defined like a getter (String getSomeMethod()). Other than that it works. Thanks!
Matthias Hryniszak
A: 

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).

Matthias Hryniszak
Not sure. I've experienced that sort of behaviour before when reRendering the region where the script is. So maybe ensure that you aren't reRendering the button or the jsFunction.
Damo