Nikhil
Nikhil

Reputation: 2883

Invoking parameterised function of javascript from managed bean in jsf

I have googled it several times but i can't get a solution. I want to make javascript function call from the bean class in jsf and i get that using the following code. RequestContext.getCurrentInstance().execute("handleResize()");
and is workign fine. But I want to give two parameters to that function height and width. How can it be done ? please help

Upvotes: 0

Views: 6172

Answers (1)

BalusC
BalusC

Reputation: 1109625

You seem to fail to grasp the fact that in the context of Java/JSF, all the HTML, CSS and JavaScript code are merely plain vanilla Strings and you seem to expect that HTML/CSS/JS somehow magically runs inside Java/JSF code. This is not true. Java/JSF is a HTML/CSS/JS code producer, not executor. The webbrowser retrieves them all as one big String and then parses and executes it.

If you want to invoke a JS function with parameters supplied, like so when you would do in real JS code:

handleResize(500, 300);

And you have those values as Java variables, then you just need to make sure that you write Java code in such way that exactly the above String is produced (again, this is just Java code, no JS code):

String call = "handleResize(" + w + ", " + h + ")";

You can verify beforehand by printing it to the stdout/logger:

System.out.println(call);

It must print exactly the desired valid JS function call syntax handleResize(500, 300);.

If it does, then just pass that unmodified to RequestContext#execute().

RequestContext.getCurrentInstance().execute(call);

Upvotes: 5

Related Questions