Friday, October 8, 2010

af:message tag sample usage

af:message usage

In this post which has the source file attached also you can see that how you can use af:message in order to show errors that is specific to that a component.
as you know when you generate a general error like the below one
String msg="this is a global message.";
FacesContext ctx = getFacesContext();
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, msg, "");
the generated error would be catch by af:messages and can be shown inline or popup.
but if you want your message be shown separately for different component or part of the page you can use af:message

the af:message has the property for="" which you must assign a component id to it, then this af:message would only shows the message that is related to that component, you mainly have one af:messages in your page to catch all the messages that are not related to any component and also you can have many af:message for different part of your page.
in order to generate a message that is related to a specific component in your page you must use below code

new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Specific Error", "this error is just for panel box 1"));

panelBox.getClientId(getFacesContext()   --> is the id of the component, you can write the id component instead but using the getClientId is error free and standard.
you can see a sample application developed in in the attachment to this post.

1 comment:

  1. Hi,
    I tried to use this in a popup. The message does not show up. an you help me?