0

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

1 Answer 1

1

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;");
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Rostislav, thanks for your hint ! just a question: since I need to reload the same page, shouldn't I use window.location.reload(true) ?
I think that you have a method call is triggered by click some button, so try to turn of AJAX and page will reload automatically. For primefaces buttons use ajax="false" attribute.
Hi Rostislav, thanks, I will give it a try. Best Eduardo
@ekremer: please accept the answer. It is correct and valid. Other users can find it easier then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.