Portal Showcase

FacesRequestContext

FacesRequestContext is a ThreadLocal singleton like FacesContext which can be called on the server to add scripts on the client.

General Usage

A script can be added to the response via FacesRequestContext.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="#{facesRequestContextBacking.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']}" value="#{facesRequestContextBacking.email}" required="true" />
						<h:message for="text" />
						<h:commandButton actionListener="#{facesRequestContextBacking.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 FacesRequestContextBacking implements Serializable {

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

	private String email;

	public void closeDialog() {

		if (email != null) {

			com.liferay.faces.util.context.FacesRequestContext facesRequestContext = FacesRequestContext
				.getCurrentInstance();
			facesRequestContext.addScript("$('.modal').modal('hide');");
		}
	}

	public String getEmail() {
		return email;
	}

	public void openDialog() {

		FacesRequestContext facesRequestContext = FacesRequestContext.getCurrentInstance();
		facesRequestContext.addScript("$('.modal').modal('show');");
	}

	public void setEmail(String email) {
		this.email = email;
	}
}
Liferay Faces Bridge Implementation 4.0.0 + Liferay Faces Portal 3.0.0 + Showcase Common 3.0.0 + Liferay Faces Util 3.0.0 + Mojarra 2.2.13