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

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.
主站蜘蛛池模板: 嘉荫县| 潍坊市| 大宁县| 金塔县| 瓮安县| 左云县| 宁夏| 任丘市| 芜湖县| 乌拉特中旗| 抚顺市| 昌图县| 固原市| 大兴区| 兴文县| 绍兴县| 汉沽区| 万年县| 田林县| 长宁区| 盈江县| 长春市| 乌拉特中旗| 华容县| 苏尼特右旗| 阳朔县| 盱眙县| 泸定县| 蛟河市| 南平市| 湛江市| 宁城县| 武隆县| 吉木萨尔县| 土默特右旗| 城步| 安国市| 南郑县| 辰溪县| 永善县| 江西省|