Blogs

Blogs

New Liferay Faces Release - FacesRequestContext

New Liferay Faces Release - FacesRequestContext

Update: There is now a runnable example of the FacesRequestContext in the Liferay Faces Showcase.

The Liferay Faces GA6 release contains many new Alloy and Portal components, which members of the Liferay Faces Team have blogged about. But components are not the only new features included in the latest release of Liferay Faces. The Liferay Faces Team has added the FacesRequestContext,1 a new feature which facilitates adding client scripts programmatically on the server-side.

FacesRequestContext is a ThreadLocal singleton like FacesContext which can be called on the server to add scripts on the client. For example, to execute an alert() on the client-side from a managed bean, you can use FacesRequestContext.addScript() like so:

FacesRequestContext.getCurrentInstance().addScript("alert('Added in managed bean.');");

In the response of the current request, the script will be written at the bottom of the page.2 If you need to run a script programmatically from a managed bean or some other server-side code, you can call FacesRequestContext.addScript() to add the script in the response.

Here’s a more robust example.3 The following dialog has a form which requires that the user write their name:

<alloy:dialog clientKey="dialog" hideIconRendered="false">
    <alloy:form>
        <alloy:inputText required="true" value="#{bean.name}" />
        <alloy:commandButton action="#{bean.action}"
            render="@form" value="Submit" />
    </alloy:form>
</alloy:dialog>

If the user has submitted their name, the dialog should be closed, and if the user has clicked submit without writing their name, the dialog should remain open. In the managed bean, FacesRequestContext.addScript() can be called to close the dialog when a valid name has been submitted:

public void action() {
    if (name != null) {
        FacesRequestContext.getCurrentInstance()
            .addScript("Liferay.component('dialog').hide();");
    }
}

Now the dialog will only close when the user’s name is not null.

FacesRequestContext is one of the many new features added in Liferay Faces GA6. Please check out the other Liferay Faces blogs by Neil, Juan, and Vernon, the Liferay Faces Showcase, the Liferay Faces page on dev.liferay.com, the Liferay Faces VDLdoc, and the Liferay Faces JavaDoc for more details about all the features of Liferay Faces.

  1. Unfortunately, there is not currently a showcase page for FacesRequestContext. However, the next release of the Liferay Faces Showcase will demonstrate a live working example of FacesRequestContext (see FACES-2504 for more details).

  2. In a full page response, the script will be written in a <script> tag which will be rendered immediately before the closing </body> tag. In an Ajax partial response, the script will be rendered in the the <eval> section of the partial response.

  3. This example was inspired by Nestor Cruz’s forum post which had a similar example.