The <apex:commandLink> component is one of the most important components of Visualforce. When the user presses the link, the action defined by the controller executes. Then either the page navigates to a different page or refreshes the current page based on the returned PageReference's variable.
<!-- create apex page with custom controller-->
<apex:page controller="ApexCommandLinkController">
<!-- use apex form to show the accounts and contacts table-->
<apex:form >
<apex:pageBlock title="Account/Contact Information">
<!-- command button that is responsible for showing accounts in the table-->
<apex:commandLink action="{!getAccounts}" value="Show Accounts"/>
<!-- command button that is responsible for showing contacts in the table-->
<apex:commandLink action="{!getContacts}" value="Show Contacts"/>
<apex:pageBlockSection rendered="{!IF(accounts.size>0, true, false)}">
<!-- create a table for showing accounts information -->
<apex:pageBlockTable value="{!accounts}" var="acc">
<apex:column value="{!acc.Name}"/>
<apex:column value="{!acc.AccountNumber}"/>
<apex:column value="{!acc.Phone}"/>
<apex:column value="{!acc.Rating}"/>
<apex:column value="{!acc.Website}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<apex:pageBlockSection rendered="{!IF(contacts.size>0, true, false)}">
<!-- create a table for showing contacts information -->
<apex:pageBlockTable value="{!contacts}" var="con">
<apex:column value="{!con.FirstName}" />
<apex:column value="{!con.LastName}"/>
<apex:column value="{!con.AccountId}"/>
<apex:column value="{!con.Email}"/>
<apex:column value="{!con.Phone}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
ApexCommandLinkController.apxc