简体   繁体   中英

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). 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). It seems like the 'page reload' lines prevented the JavaScript code from executing. 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. it can't work.

Try to do something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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