简体   繁体   English

JSF:强制页面重新加载时,无法从Managed Bean调用javascript代码

[英]JSF: calling javascript code from Managed Bean not working when forcing page reload

I need to execute a JavaScript code from a JSF Managed Bean 's method that is basically to click a button on a facelet (.xhtml file). 我需要从JSF Managed Bean的方法执行JavaScript代码,该方法基本上是单击facelet(.xhtml文件)上的按钮。 All this works perfectly by using PrimeFaces' RequestContext.execute("{js here}") , however it no longer works when adding some 'page reload' code lines by using ExternalContext... (please see code below). 所有这些都可以通过使用PrimeFaces的RequestContext.execute(“ {js here}”)完美地实现,但是当通过使用ExternalContext添加一些“页面重新加载”代码行时,它将不再起作用(请参见下面的代码)。 It seems like the 'page reload' lines prevented the JavaScript code from executing. 似乎“页面重新加载”行阻止了JavaScript代码的执行。 Any hints is welcome ! 任何提示都欢迎!

public void myMethod() throws IllegalStateException, Exception {

    RequestContext rc = RequestContext.getCurrentInstance();.           

    rc.execute("$('#myButton').click();");

    try {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        ec.redirect(((HttpServletRequest) ec.getRequest()).getRequestURI());

    } catch (IOException e) {
        log.error("Error", e);
    }

RequestContext is for the current request, a redirect is a another request. RequestContext用于当前请求,重定向是另一个请求。 it can't work. 它行不通。

Try to do something like this: 尝试做这样的事情:

RequestContext rc = RequestContext.getCurrentInstance();
rc.execute("$('#myButton').click(); window.location = newURL;");

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

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