简体   繁体   中英

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

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 String s 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM