Tuesday, January 3, 2012

ADF: How to show af:message programatically


ADF Faces uses the standard JSF messaging API. JSF supports a built-in framework for messaging by allowing FacesMessage instances to be added to theFacesContext object using the addMessage(java.lang.String clientId, FacesMessage message) method. 
In general there are two types of messages that can be created:component-level messages, which are associated with a specific component based on any client ID that was passed to the addMessage method, and global-level messages, which are not associated with a component because no client ID was passed to the addMessage method.
in this post, I will show how to show af:message programatically.
  • global level message:
To show a global level message, use this method:
public String showMessage() {
        String messageText=”A prgramatic af:message”;
        FacesMessage fm = new FacesMessage(messageText);
        /**
         * set the type of the message.
         * Valid types: error, fatal,info,warning
         */
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, fm);
        return null;
    }
the code above will show the message in pop-up dialog as shown below.
pop-up global message

However, you can show the global message inline with the page, what you want to do is to change the inline attribute of the af:messages in your page, this component is created automatically for you when you create a page. 
The inline attribute controls whether to render the message list inline with the page or in a popup window, the default value is false. Normally the messages are rendered in a pop up. If this attribute is set to true, the messages list will be rendered inline with the page. 
To find the af:messages, open your page in the main window, from the structure window you can find it under f:view,af:document nodes. Your global message will be shown inline with page as shown below.
global message - inline with the page
  • component-level message:
To show a message inline with a component (i.e. associated with a specific UI component) you need to expose the UI component in the managed bean using the Binding property, then you can use this method:
public String showMessage() {
        String messageText=”A prgramatic af:message”;
        FacesMessage fm = new FacesMessage(messageText);
        /**
         * set the type of the message.
         * Valid types: error, fatal,info,warning
         */
        fm.setSeverity(FacesMessage.SEVERITY_INFO);
        FacesContext context = FacesContext.getCurrentInstance();
        //departmentName is the binding property for our field.
        context.addMessage(getDepartmentName().getClientId(context), fm);
        return null;
    }
The code above will show the message associated with department name field as shown below:
component-level messages 
Reference: Credits goes to original author Mohamed Jabr

No comments :

Post a Comment