Invoking parameterised function of javascript from managed bean in jsf - Stack Overflow

admin2025-04-19  0

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

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

Share Improve this question asked Sep 12, 2013 at 6:31 NikhilNikhil 2,8839 gold badges35 silver badges59 bronze badges 5
  • Maybe RequestContext.getCurrentInstance().execute("handleResize(100, 200)");? – Thilo Commented Sep 12, 2013 at 6:41
  • Not static values. I tried with static values and it work. But i want to pass dynamic values. – Nikhil Commented Sep 12, 2013 at 6:49
  • @nik Give us an example. – Rong Nguyen Commented Sep 12, 2013 at 7:28
  • @RongNK like int h=500,w=300; RequestContext.getCurrentInstance().execute("handleResize(h, w)"); – Nikhil Commented Sep 12, 2013 at 8:56
  • @nik String str = "handleResize(" + h + "," + w + ")", and then RequestContext.getCurrentInstance().execute(str) – Rong Nguyen Commented Sep 12, 2013 at 8:58
Add a ment  | 

1 Answer 1

Reset to default 5

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);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745041428a281563.html

最新回复(0)