从托管 bean 调用 javascript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19314735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:01:28  来源:igfitidea点击:

call javascript from managed bean

javajavascriptjsf-2

提问by user2644886

I want to clear form hidden value in JSF page from managedbean class .so i used to call a method in that i used this code to call javascript but it throws java.lang.NullPointerException below is the code.

我想从 managedbean 类中清除 JSF 页面中的表单隐藏值。所以我曾经调用一个方法,因为我使用这段代码来调用 javascript 但它抛出 java.lang.NullPointerException 下面是代码。

FacesContext facesContext = FacesContext.getCurrentInstance();
    ExtendedRenderKitService service = 
              Service.getRenderKitService(facesContext, ExtendedRenderKitService.class);
            service.addScript(facesContext, "alert('foo');");

the error is thrown in the line where alert is called .can someone help me please.even i tried to call a function in JSF page to reset values...it gave me same error.

错误是在调用警报的行中抛出的。有人可以帮我吗。即使我试图在 JSF 页面中调用一个函数来重置值...它给了我同样的错误。

回答by mstzn

if you use primefaces,you can use Request Context component of primefaces.

如果使用primefaces,则可以使用primefaces 的Request Context 组件。

RequestContext

请求上下文

if you dont want ,you can check this out

如果你不想,你可以看看这个

without primefaces

没有素面

回答by Achmad Hanafi

You can use Request Context componentof primefaces

您可以使用primefaces 的Request Context 组件

回答by Sanketik Gote

RequestContext.getCurrentInstance().execute("jsMethod();");

RequestContext.getCurrentInstance().execute("$('#ID').hide();");

RequestContext.getCurrentInstance().execute("$('#ID').hide();");

RequestContext.getCurrentInstance().execute("PF('WIDGET').show();");

You can execute any javascript/jquery/primefaces code inside execute method.

您可以在 execute 方法中执行任何 javascript/jquery/primefaces 代码。