Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I am working through the Visualforce Developers Guide and during the section on Custom Controllers, an example performs a search stating that it also executes the setter but does not explain why. Would appreciate if someone can help me understand the rule behind this. Here is the section describing and the VF & Controller code from the VF Developers Guide:

"The associated controller includes getter and setter methods for the search box input, and then uses the search text to issue a SOSL query when the user clicks Go!. Although the markup doesn’t explicitly call the search text setter method, it executes before the doSearch action method when a user clicks the command button:

<apex:page controller="theController">
  <apex:form>
    <apex:pageBlock mode="edit" id="block">
      <apex:pageBlockSection>
        <apex:pageBlockSectionItem>
          <apex:outputLabel for="searchText">Search Text</apex:outputLabel>
            <apex:panelGroup>
              <apex:inputText id="searchText" value="{!searchText}"/>
                <apex:commandButton value="Go!" action="{!doSearch}"  <!-- search here -->
                   rerender="block" status="status"/>
        </apex:panelGroup>
        </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
      <apex:actionStatus id="status" startText="requesting..."/>
      <apex:pageBlockSection title="Results" id="results" columns="1">
        <apex:pageBlockTable value="{!results}" var="l" rendered="
            {!NOT(ISNULL(results))}">
          <apex:column value="{!l.name}"/>
          <apex:column value="{!l.email}"/>
          <apex:column value="{!l.phone}"/>
        </apex:pageBlockTable>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
</apex:page>

The following class is the controller for the page markup above:

public class theController {
  String searchText;
  List<Lead> results;

public String getSearchText() {
  return searchText;
  }

public void setSearchText(String s) {  // dosearch calls setter here
  searchText = s;
  }

public List<Lead> getResults() {
  return results;
  }

public PageReference doSearch() {
  results = (List<Lead>)[FIND :searchText RETURNING Lead(Name, Email, Phone)][0];
  return null;
  }
}
share|improve this question
    
sorry that stackexchange removed nice formatting of my code .... (think I fixed the formatting) –  bruce frager May 11 at 1:20

1 Answer 1

up vote 4 down vote accepted

It's doing exactly what it says: it's setting the value through the use of the setter and getter methods. See the documentation on the Visualforce life cycle, but in summary, Visualforce basically does this:

  • Deserializes any postback information (the view state)
  • Calls and constructors and static methods
  • Calls any setters that are bound to rendered elements (which may be hidden, but still present)
  • Performs the action for the transaction
  • Calls any getters that are bound to rendered elements
  • Generates the view state
  • Outputs the page

In your example code, that means that clicking the search button does this:

  • Deserializes the view state
  • Calls setSearchText
  • Calls doSearch
  • Calls getSearchText and getResults (in an indeterminate order)
  • Generates the view state
  • Renders the search results

The reason why setters are called with each transaction is so that any business logic associated with the setter can be performed (e.g. normalizing or validating data, etc). While this page could have been written with so-called "automatic getters and setters", this code is meant to illustrate that more complex logic can also be incorporated, as well as the fact that "setSearchText" and "getSearchText" are called without the programmer explicitly calling those functions-- nowhere in your page markup do you have {!setSearchText}, for example, because it is automatically bound by the Visualforce runtime engine.

share|improve this answer
    
Excellent explanation and also helped clarify the section on Visualforce lifecycle which came later. –  bruce frager May 11 at 17:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.