Reputation: 311
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);
}
Upvotes: 0
Views: 1156
Reputation: 91
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;");
Upvotes: 1