From the course: Java EE 8: JavaServer Faces JSF 2.3

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Execute JavaScript from the backing bean

Execute JavaScript from the backing bean

From the course: Java EE 8: JavaServer Faces JSF 2.3

Start my 1-month free trial

Execute JavaScript from the backing bean

- Okay, so, this one has been available in some form since at least version 2.1 of JSF. The ability to trigger JavaScript on the webpage from the backing bean has existed in a roundabout hack-y way. JSF 2.3 brings that functionality out of the shadows. You can now cleanly execute client-side scripts from the backing bean. Here's what that looks like. I'm starting from this request called BackingBean that I've pre-created. I'll add a method here to handle the JavaScript's triggering. So, I'm going to come down here, have a public void method called triggerJs. All right, following that, I need to get ahold of the FacesContext object. I'll do that by way of injection, so @Inject, make that private, FacesContext, facesContext. Wonderful, let's fix the imports, import that, import FacesContext, there we have it. From there, it's easy street. I'm going to come down into my triggerJs method, and use the facesContext as…

Contents