Expamle for Getter method using VFpage and 'apex class:


Write an example for getter method using Visualforce and apex class-


Public class Example
{
    String name;

Public String getName( )
 {
    return 'sam';
      }
  }


Example page:
     
    <apex:Page controller="Example">
    <apex:outputlabel> {!name} </apex:outputlabel>
     <apex:page>

In the above programme when  the page is loaded first it creates an object for the example class.
When outputlabel calls {!name} in the Vf Page it invokes getName() method in the controller class.
which will return 'san'.


Output: 'sam'


Apex Class  Example:

Public class Example
  {
         String name;

 Public Example ( )
  {
   name='hari'';
           }
Public String getName()
 {
     return name;
    }
 }
}



Example Page:


 <apex:page controller="Example">
<apex:ouputlabel> your name is {!name} </apex:outputlabel>
<apex:page>

Output:
                       'Hari'


writing the values into apex variables from VisualforcePage:

      This is called red/write operation on the variable.

Ex:     
          {!age}
           Public Void setage(Integer age)
                 {
           this.age=age;
                         }

Ex: {!name}
     
       PublicVoid setName(String name)
         {
             this.name=name;
              }

write an Apex class to return the value to Visualforce Page:

Apex class:-
   
     Public class Example
        {
          Public Integer age;
         Public Example ()
              {
                  age=10;
                 }

          Public Integer getAge()
           {
                  return age;
               }
            Public string getName()
          {
           return 'sam kumar';
             }

          }


VF PAGE:
    <apex:page controller="Example">
     <apx:outputlLabel> {!age} <apex:outputLabel>
      <apex:outputLabel> {!name} </apex:outputLabel>
      <apex:page>

















No comments:

Post a Comment