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

Setting a value into a controller property

Visualforce controllers are often re-used 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 download 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 download 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 | 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 download 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 | 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 | 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 download 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 | 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.

While, 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.
主站蜘蛛池模板: 宜兰市| 固镇县| 工布江达县| 出国| 乌审旗| 页游| 龙海市| 黄平县| 黑龙江省| 会同县| 正镶白旗| 莱西市| 乐山市| 孟连| 汨罗市| 安国市| 开封市| 佛冈县| 察隅县| 前郭尔| 舒城县| 英德市| 泸定县| 陇川县| 利津县| 会宁县| 巨野县| 石渠县| 新昌县| 揭阳市| 社旗县| 于都县| 漳州市| 巴彦淖尔市| 香河县| 山阳县| 马尔康县| 宁国市| 贡嘎县| 东丽区| 从化市|