Friday, February 17, 2017

Displaying messages in ADF Page



Displaying messages in ADF Page


In this article, we shall see different ways of adding messages on ADF page.

Scenario 1:

Showing message as pop up
Following code can be used for displaying error message on ADF page as a pop up message as shown in below screenshot:-



    public String showMessage() {
      
        // Below code shows message on the page as pop up
        //SCENARIO 1:  client Id is null, as it is not associated to any UI component

        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg1 = new FacesMessage("This is sample message");

        // set severity of the message
        msg1.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(null,msg1 );
   }

Scenario 2:

For displaying the message inline in the page i.e. as shown in below screenshot,:-



Look for af:messages component in JSF page and make “inline” property to “True” as shown in below screenshot:-




Scenario3:
In this scenario we will be showing message attached to a UI Component, in this case userName



Use below code for component specific message
    public String showMessage() {
       
        //SCENARIO 2:  Message has to be associated with UI component
        // Below code show message on the page associated to User name input text
        FacesContext context = FacesContext.getCurrentInstance();
        FacesMessage msg2 = new FacesMessage("This is sample message for name");

        // set severity of the messagge
        msg2.setSeverity(FacesMessage.SEVERITY_ERROR);
        context.addMessage(getUserName().getClientId(),msg2 );
        return "success";
    }

Scenario 4:

Displaying detailed message with HTML content as shown below:-



Use below code for displaying message in HTML format

  public String showMessage() {
       
        //SCENARIO 4:  Message has to be associated with UI component and details shown in HTML
        // Below code show message on the page
        FacesContext context = FacesContext.getCurrentInstance();

        FacesMessage msg2 = new FacesMessage("This is sample message for name");

        // set severity of the message
        msg2.setSeverity(FacesMessage.SEVERITY_ERROR);
        msg2.setDetail("<html><body><p>If you continously get this error, please contact System                                 Administrator or log a ticket by clicking on this link <a href=\"http://www.google.com\">Log                   Ticket</a></p></body></html>");
        context.addMessage(getUserName().getClientId(),msg2 );
        return "success";

}

2 comments: