Close

PrimeFaces - Dialog Messages

[Last Updated: Dec 20, 2017]

Following example shows how to invoke message dialogs programmatically.

Example

JSF page

src/main/webapp/index.xhtml

<h:body>
    <h2>PrimeFaces - Message Dialog </h2>
    <h:form>
        <p:commandButton value="Save Data" actionListener="#{mainBean.saveData}" />
     </h:form>
</h:body>

The Main bean

@ManagedBean
@ViewScoped
public class MainBean {

  public void saveData() {
      FacesMessage message = null;
      if(Math.random()<0.5){//simulating success/failure of saving data
          message=new FacesMessage(FacesMessage.SEVERITY_ERROR,
                  "Error during saving data", "Data cannot be saved...");
      }
      else{
          message=new FacesMessage(FacesMessage.SEVERITY_INFO,
                  "Data saved", "Data has been saved successfully...");
      }
      RequestContext.getCurrentInstance().showMessageInDialog(message);
  }
}

On Clicking the save data button various times:

Example Project

Dependencies and Technologies Used:

  • primefaces 6.1 primefaces
  • jsf-api 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • jsf-impl 2.2.14: This is the master POM file for Oracle's Implementation of the JSF 2.2 Specification.
  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

Dialog Messages Example Select All Download
  • dialog-message
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • MainBean.java
          • webapp
            • WEB-INF

    See Also