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

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://xmlns.jcp.org/jsf/html"
  2. xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  3.  
  4. <h:outputScript library="bootstrap" name="js/jquery.min.js" target="head" />
  5. <h:outputScript library="bootstrap" name="js/bootstrap.min.js" target="head" />
  6. <h:form>
  7. <h:commandButton actionListener="#{facesContextHelperUtilBacking.openDialog}"
  8. value="#{i18n['show-modal']}">
  9. <f:ajax />
  10. </h:commandButton>
  11. </h:form>
  12. <h:panelGroup styleClass="modal showcase-modal-dialog" style="display: none;">
  13. <h:panelGroup layout="block" styleClass="modal-dialog">
  14. <h:panelGroup layout="block" styleClass="modal-content">
  15. <h:panelGroup layout="block" styleClass="modal-body">
  16. <h:form>
  17. <h:inputText id="text" label="#{i18n['email']}"
  18. validator="#{inputTextBackingBean.emailAddressValidator}"
  19. validatorMessage="#{i18n['validator-message']}"
  20. value="#{facesContextHelperUtilBacking.email}" required="true" />
  21. <h:message for="text" />
  22. <h:commandButton actionListener="#{facesContextHelperUtilBacking.closeDialog}"
  23. value="#{i18n['submit']}">
  24. <f:ajax execute="@form" render="@form" />
  25. </h:commandButton>
  26. </h:form>
  27. </h:panelGroup>
  28. </h:panelGroup>
  29. </h:panelGroup>
  30. </h:panelGroup>
  31. <h:outputScript target="body">
  32. $('.modal').modal({ backdrop: 'static', keyboard : false, show : false });
  33. </h:outputScript>
  34.  
  35. </ui:composition>
  1. @ManagedBean
  2. @ViewScoped
  3. public class FacesContextHelperUtilBacking implements Serializable {
  4.  
  5. // serialVersionUID
  6. private static final long serialVersionUID = 5123157520254209271L;
  7.  
  8. private String email;
  9.  
  10. public void closeDialog() {
  11.  
  12. if (email != null) {
  13. com.liferay.faces.util.context.FacesContextHelperUtil.addScript("$('.modal').modal('hide');");
  14. }
  15. }
  16.  
  17. public String getEmail() {
  18. return email;
  19. }
  20.  
  21. public void openDialog() {
  22. com.liferay.faces.util.context.FacesContextHelperUtil.addScript("$('.modal').modal('show');");
  23. }
  24.  
  25. public void setEmail(String email) {
  26. this.email = email;
  27. }
  28. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20