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

I have googled it several times but i can't get a solution. I want to make javascript function cal

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745041427a4607829.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信