官术网_书友最值得收藏!

Setting a value into a controller property

Visualforce controllers are often reused across pages with minor variations in behavior specific to the page, for example, displaying accounts of a particular type. While the controller can detect the page that it is being used by and alter its behavior accordingly, this is not a particularly maintainable solution, as use of the controller in any new page would require changes to the Apex code and renaming a page would break the functionality.

A better mechanism is for the page to set the values of properties in the controller to indicate the desired behavior. In this recipe, we will create a custom component that takes two attributes: a value and the controller property to set the value into. Two Visualforce pages with a common controller will also be created to demonstrate how the component can be used to change the behavior of the controller to suit the page.

Getting ready

This recipe does not require any Apex controllers, so we can start with the custom component.

How to do it...

  1. Navigate to the Visualforce Components setup page by clicking on Your Name Setup Develop Components.
  2. Click on the New button.
  3. Enter SetControllerProperty in the Label field.
  4. Accept the default SetControllerProperty that is automatically generated for the Name field.
  5. Paste the contents of the SetControllerProperty.component file from the code downloaded into the Visualforce Markup area and click on the Save button.
  6. Next, create the custom controller for the Visualforce pages by navigating to the Apex Classes setup page by clicking on Your Name Setup Develop Apex Classes.
  7. Click on the New button.
  8. Paste the contents of the AccountsTypeController.cls Apex class from the code downloaded into the Apex Class area.
  9. Click on the Save button.
  10. Next, create the first Visualforce page by navigating to the Visualforce setup page by clicking on Your Name | Setup | Develop Visualforce Pages.
  11. Click on the New button.
  12. Enter AccountsType1 in the Label field.
  13. Accept the default AccountsType1 that is automatically generated for the Name field.
  14. Paste the contents of the AccountsType1.page file from the code downloaded into the Visualforce Markup area and click on the Save button.
  15. Navigate to the Visualforce setup page by clicking on Your Name Setup Develop Visualforce Pages.
  16. Locate the entry for the AccountsType1 page and click on the Security link.
  17. On the resulting page, select which profiles should have access and click on the Save button.
  18. Finally, create the second Visualforce page by navigating to the Visualforce setup page by clicking on Your Name Setup Develop Visualforce Pages.
  19. Click on the New button.
  20. Enter AccountsType2 in the Label field.
  21. Accept the default AccountsType2 that is automatically generated for the Name field.
  22. Paste the contents of the AccountsType2.page file from the code downloaded into the Visualforce Markup area and click on the Save button.
  23. Navigate to the Visualforce setup page by clicking on Your Name Setup Develop Visualforce Pages.
  24. Locate the entry for the AccountsType2 page and click on the Security link.
  25. On the resulting page, select which profiles should have access and click on the Save button.

How it works...

Opening the first Visualforce page in your browser displays a list of accounts whose type is Customer - Direct: https://<instance>/apex/AccountsType1.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

Opening the second Visualforce page displays a list of accounts whose type is Customer - Channel: https://<instance>/apex/AccountsType2.

Here, <instance> is the Salesforce instance specific to your organization, for example, na6.salesforce.com.

The SetControllerProperty custom component assigns the value attribute to the controller property attribute:

<apex:component > 
    <apex:attribute name="from" type="String" assignTo="{!to}"  
      description="The value to set"/> 
    <apex:attribute name="to" type="String"  
      description="The controller property to set the value into"/> 
</apex:component> 

The Visualforce pages set the type of account to be retrieved into the controller property via the custom component:

<c:SetControllerProperty from="Customer - Direct"  
to="{!accType}" /> 

See also

  • The Updating attributes in component controllers recipe in this chapter shows how a custom component can update an attribute that is a property of the enclosing page controller.
  • The Passing attributes to components recipe in this chapter shows how an sObject may be passed as an attribute to a custom component.
主站蜘蛛池模板: 图木舒克市| 隆德县| 沙河市| 黑龙江省| 汽车| 耒阳市| 通化县| 克拉玛依市| 大姚县| 庄浪县| 青田县| 佛冈县| 凤凰县| 湖北省| 兴化市| 九龙城区| 酉阳| 泰顺县| 恩施市| 西吉县| 曲麻莱县| 朔州市| 姜堰市| 铁岭市| 文水县| 苍南县| 鄂伦春自治旗| 湛江市| 孟津县| 文昌市| 巴南区| 岢岚县| 格尔木市| 东光县| 工布江达县| 尉氏县| 常山县| 锦州市| 景宁| 名山县| 上思县|