Wednesday, January 7, 2009

ADF 10g : How to show faces message both globally and below the UI component.

Hi all,
Welcome again, after a long time I am finally back on blogging.. in the time of economy recession!I think I was not able to update my blog from almost last 7-8 months because of various resons, joining new company, working new technologies of Oracle Fusion and working on multiple projects simultaneously.

In the past 6 months i have worked as an architect on two ADF 10G projects. When I compare ADF with my OA Framework experience, I find ADF to be much more challenging as well as flexible to mold as per the requirement in your project.

From now I would be coming up with small articles on ADF which will be helpful, to develop useful APIs by product architects, so that other developers would be able to use them , without worrying about the actual implementation.

One task in ADF which looks very easy, is showing of different jsf messages.One great feature of ADF is that Faces messages can be shown globally at the top of page, as well as just below the UI ADF bean, which is very illustrative for users.

CASE I:Showing Global Message in an ADF page
1)af:messages tag is used for showing global messages in an ADF page.This tag is created automatically whenever you drop an input widget from the
Data Control Palette. However, if you need to insert the tag, simply insert the
following code within the afh:body tag:

2. In the Property Inspector set the following attributes:
■ globalOnly: By default ADF Faces displays global messages (i.e., messages
that are not associated with components) followed by individual component
messages. If you wish to display only global messages in the box, set this
attribute to true. Component messages will continue to display with the
associated component.
■ message: The main message text that displays just below the message box
title, above the list of individual messages.
■ text: The text that overrides the default title of the message box.

Code for displaying global Faces message :
//You need to enter error description
String error_decp="";
//You need to enter error detail
String error_detail="";
//Get current instance of faces context
FacesContext fc = FacesContext.getCurrentInstance();
//Creating faces message with description and detail
FacesMessage message = new FacesMessage(error_descp, error_detail);
//setting severity of message,i.e. message type
//Adding message to faces context.
fc.addMessage(null, message);

CASE II:Showing Faces Message in an ADF UI COMPONENT along with global error message:

a)JSF Validator :
This approcah is typically used when you have same kind of validation repeatdly in your code. Lets take a very generic example you need to verify that the entered number should have precision of 2 decimal places, i.e. whatever number user enters should have 2 digits after decimal.You can write a jsf validator for this register in the application and use in any othe pages. If you are new to JSF validator just search google you will find numerous examples of how to write a JSF validator.

b) Directly adding message with UI component based of custom code in backing bean:

i)In complex data UI components like table:
String global_error=""; //Error that need to be shown at top of the page
String component_error="";//Error that need to be shown on the component
String formId="";// Id of Form bean
String TableId="";//Id of table bean
String rowIndex="";//String value of row index
String message="";//message type

FacesContext fc = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(global_error, component_error);
fc.addMessage(formId+":"+TableId+":"+String.valueOf(rowIndex)+":"+ComponentId, message);

ii)In simple data UI components like input text:
FacesContext fc = FacesContext.getCurrentInstance();
FacesMessage message = new FacesMessage(global_error, component_error);
fc.addMessage(formId+":"+ComponentId, message);

I hope the above article has provided a brief description about how to show global and UI specific messages , you can use in ADF via JSF APIs.