FacesContextHelperUtil

FacesContextHelperUtil is a convenience utility that reduces the boilerplate code required to add messages or scripts, find components, obtain request attributes, and more.

General Usage

A script can be added to the response via FacesContextHelperUtil.addScript(java.lang.String). The script will be written out at the bottom of the page (or in the <eval> section in a partial response) in the response of the current request.

Source Code

<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
	xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">

	<h:outputScript library="bootstrap" name="js/jquery.min.js" target="head" />
	<h:outputScript library="bootstrap" name="js/bootstrap.min.js" target="head" />
	<h:form>
		<h:commandButton actionListener="#{facesContextHelperUtilBacking.openDialog}"
			value="#{i18n['show-modal']}">
			<f:ajax />
		</h:commandButton>
	</h:form>
	<h:panelGroup styleClass="modal showcase-modal-dialog" style="display: none;">
		<h:panelGroup layout="block" styleClass="modal-dialog">
			<h:panelGroup layout="block" styleClass="modal-content">
				<h:panelGroup layout="block" styleClass="modal-body">
					<h:form>
						<h:inputText id="text" label="#{i18n['email']}"
							validator="#{inputTextBackingBean.emailAddressValidator}"
							validatorMessage="#{i18n['validator-message']}"
							value="#{facesContextHelperUtilBacking.email}" required="true" />
						<h:message for="text" />
						<h:commandButton actionListener="#{facesContextHelperUtilBacking.closeDialog}"
							value="#{i18n['submit']}">
							<f:ajax execute="@form" render="@form" />
						</h:commandButton>
					</h:form>
				</h:panelGroup>
			</h:panelGroup>
		</h:panelGroup>
	</h:panelGroup>
	<h:outputScript target="body">
		$('.modal').modal({ backdrop: 'static', keyboard : false, show : false });
	</h:outputScript>

</ui:composition>
@ManagedBean
@ViewScoped
public class FacesContextHelperUtilBacking implements Serializable {

	// serialVersionUID
	private static final long serialVersionUID = 5123157520254209271L;

	private String email;

	public void closeDialog() {

		if (email != null) {
			com.liferay.faces.util.context.FacesContextHelperUtil.addScript("$('.modal').modal('hide');");
		}
	}

	public String getEmail() {
		return email;
	}

	public void openDialog() {
		com.liferay.faces.util.context.FacesContextHelperUtil.addScript("$('.modal').modal('show');");
	}

	public void setEmail(String email) {
		this.email = email;
	}
}
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20