0

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!

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

3 Comments

Roughly yes, but I'd prefer the result be returned by someMethod rather than by myBean.getSomeString().
I haven't tried it but you could drop the action and put the someMethod() in the data?
I did that. it's just that the data method must be defined like a getter (String getSomeMethod()). Other than that it works. Thanks!
0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.