How to show af:message programatically

Environment (JDeveloper 11.1.2.0.0, ADF Faces)
ADF Faces uses the standard JSF messaging API. JSF supports a built-in framework for messaging by allowing FacesMessage instances to be added to the FacesContext 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

About these ads

13 Comments

Filed under ADF

13 responses to “How to show af:message programatically

  1. DC

    This example works perfectly for me but how to I clear the message?

  2. DC

    I am working in 10g (10.1.3.5) my global af:message is embedded in the page. I can programmatically set the message just as you did above but I can not find a way to clear it.

  3. Hi Mohammad,

    Thanx for the tutorial, I’m wondering if there is a way to customize the information message box to specific width and height, as you can see in pop-up global message above the height of the popup is big, I want to make it more smaller and center in the screen.

    Regards,

  4. Manish Pandey

    hi jabr,

    when i inline property of af:message is set “true” jsf page not display on full size(it is just set on corner of browser window and whole page not display)

  5. stathis

    can i handle the ok button on inside the af message?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s