h:commandButton

HtmlCommandButton is a UICommand component that renders a styleable HTML <input> element. The default type is submit. The component must be a child of h:form in order to trigger the action or actionListener.

Immediate Usage

When the immediate attribute is true, the action and actionListener callbacks execute during the APPLY_REQUEST_VALUES phase of the JSF lifecycle, rather than the normal INVOKE_APPLICATION phase.

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. <!-- Example 1: ActionListener execution when immediate is true -->
  5. <h:form>
  6. <h:messages globalOnly="true" styleClass="feedback" />
  7. <h:commandButton actionListener="#{commandBackingBean.actionListener}" immediate="true"
  8. value="#{i18n['submit']}">
  9. <f:ajax render="@form" />
  10. </h:commandButton>
  11. </h:form>
  12.  
  13. <!-- Example 2: ActionListener execution when immediate is false (the default) -->
  14. <h:form>
  15. <h:messages globalOnly="true" styleClass="feedback" />
  16. <h:commandButton actionListener="#{commandBackingBean.actionListener}" value="#{i18n['submit']}">
  17. <f:ajax execute="@form" render="@form" />
  18. </h:commandButton>
  19. </h:form>
  20.  
  21. </ui:composition>
  1. @ManagedBean
  2. @RequestScoped
  3. public class CommandBackingBean {
  4.  
  5. private static final Logger logger = LoggerFactory.getLogger(CommandBackingBean.class);
  6.  
  7. // Injections
  8. @ManagedProperty(value = "#{commandModelBean}")
  9. private CommandModelBean commandModelBean;
  10.  
  11. public void actionListener(ActionEvent actionEvent) {
  12.  
  13. FacesContext facesContext = FacesContext.getCurrentInstance();
  14. PhaseId phaseId = facesContext.getCurrentPhaseId();
  15. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  16.  
  17. String phaseName = phaseId.toString();
  18. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  19. " phase of the JSF lifecycle.");
  20. facesContext.addMessage(null, facesMessage);
  21. }
  22.  
  23. public void ajaxListener(AjaxBehaviorEvent ajaxBehaviorEvent) {
  24.  
  25. FacesContext facesContext = FacesContext.getCurrentInstance();
  26. PhaseId phaseId = facesContext.getCurrentPhaseId();
  27. logger.debug("ajaxListener: phaseId=[{0}]", phaseId.toString());
  28.  
  29. String phaseName = phaseId.toString();
  30. FacesMessage facesMessage = new FacesMessage("The ajaxListener method was called during the " + phaseName +
  31. " phase of the JSF lifecycle.");
  32. facesContext.addMessage(null, facesMessage);
  33. }
  34.  
  35. public void attributeActionListener(ActionEvent actionEvent) {
  36.  
  37. FacesContext facesContext = FacesContext.getCurrentInstance();
  38. PhaseId phaseId = facesContext.getCurrentPhaseId();
  39. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  40.  
  41. String value = (String) actionEvent.getComponent().getAttributes().get("attribute");
  42.  
  43. String phaseName = phaseId.toString();
  44. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  45. " phase of the JSF lifecycle.The attribute value is " + value);
  46. facesContext.addMessage(null, facesMessage);
  47. }
  48.  
  49. public void attributesActionListener(ActionEvent actionEvent) {
  50.  
  51. FacesContext facesContext = FacesContext.getCurrentInstance();
  52. PhaseId phaseId = facesContext.getCurrentPhaseId();
  53. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  54.  
  55. Map<String, Object> attributes = actionEvent.getComponent().getAttributes();
  56. String value1 = (String) attributes.get("attribute1");
  57. String value2 = (String) attributes.get("attribute2");
  58.  
  59. String phaseName = phaseId.toString();
  60. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  61. " phase of the JSF lifecycle.The attributes value are " + value1 + " and " + value2);
  62. facesContext.addMessage(null, facesMessage);
  63. }
  64.  
  65. public void feedbackListener(ActionEvent actionEvent) {
  66.  
  67. String value = "";
  68.  
  69. List<UIComponent> children = actionEvent.getComponent().getChildren();
  70.  
  71. for (UIComponent uiComponent : children) {
  72.  
  73. if (uiComponent instanceof UIOutput) {
  74. value = (String) ((UIOutput) uiComponent).getValue();
  75. }
  76. }
  77.  
  78. FacesContext facesContext = FacesContext.getCurrentInstance();
  79. logger.debug("feedbackListener: You selected the '" + value + "' menu item.");
  80.  
  81. FacesMessage facesMessage = new FacesMessage("You selected the '" + value + "' menu item.");
  82. facesContext.addMessage(null, facesMessage);
  83. }
  84.  
  85. public Map<String, Object> getAttributes() {
  86. Map<String, Object> attributes = new HashMap<String, Object>();
  87. attributes.put("attribute1", "value1");
  88. attributes.put("attribute2", "value2");
  89.  
  90. return attributes;
  91. }
  92.  
  93. public void parameterActionListener() {
  94.  
  95. FacesContext facesContext = FacesContext.getCurrentInstance();
  96. PhaseId phaseId = facesContext.getCurrentPhaseId();
  97. logger.debug("actionListener: phaseId=[{0}]", phaseId.toString());
  98.  
  99. Map<String, String> requestParameterMap = facesContext.getExternalContext().getRequestParameterMap();
  100. String value = requestParameterMap.get("parameter");
  101.  
  102. String phaseName = phaseId.toString();
  103. FacesMessage facesMessage = new FacesMessage("The actionListener method was called during the " + phaseName +
  104. " phase of the JSF lifecycle.The parameter value is " + value);
  105. facesContext.addMessage(null, facesMessage);
  106. }
  107.  
  108. public void selectionListener(ActionEvent actionEvent) {
  109.  
  110. UICommand uiCommand = (UICommand) actionEvent.getComponent();
  111. Customer customer = (Customer) uiCommand.getValue();
  112. commandModelBean.setSelectedCustomer(customer);
  113. }
  114.  
  115. public void setCommandModelBean(CommandModelBean commandModelBean) {
  116. this.commandModelBean = commandModelBean;
  117. }
  118. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20