[SalesForce] Alert message on vf page

I need to display a message on vf page based on onchange of data from picklist. In my controller the method is boolean type….based on data selected from picklist which call my method…if the method return true a alert message "true" will be displayed else if it return false message will display "false". Please help with a sample code.

Best Answer

To display error messages in the Visualforce page add below tag where you want to display the error message.

Visualforce page:

<apex:pageMessages />

Apex Controller:

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.');
ApexPages.addMessage(myMsg); 

(Or)

ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,'Error Message.'));