简体   繁体   English

从jsf中的托管bean调用javascript的参数化功能

[英]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. 我想从jsf中的bean类进行javascript函数调用,我可以使用以下代码来实现。 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. 您似乎无法掌握以下事实:在Java / JSF的上下文中,所有HTML,CSS和JavaScript代码都只是普通的String并且您似乎期望HTML / CSS / JS在Java / JSF代码中以某种方式神奇地运行。 This is not true. 这不是真的。 Java/JSF is a HTML/CSS/JS code producer, not executor. Java / JSF是HTML / CSS / JS代码生产者,而不是执行者。 The webbrowser retrieves them all as one big String and then parses and executes it. Web浏览器将它们全部检索为一个大String ,然后解析并执行它。

If you want to invoke a JS function with parameters supplied, like so when you would do in real JS code: 如果要使用提供的参数来调用JS函数,例如在实际的JS代码中执行的操作时,就像这样:

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): 并且将这些值作为Java变量,然后只需确保以完全产生上述String方式编写Java代码(同样,这只是Java代码,没有JS代码):

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); 它必须精确打印所需的有效JS函数调用语法handleResize(500, 300); .

If it does, then just pass that unmodified to RequestContext#execute() . 如果是这样,则只需将未修改的内容传递给RequestContext#execute()

RequestContext.getCurrentInstance().execute(call);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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