More about Apex and Visualforce
Passing Parameters To A Visualforce Page And Between Pages
• What is a Parameter?
• A parameter is a name value pair that goes into the URL of a page. The first parameter starts with a '?' and the subsequent start with a '&'.
http://www.google.com?browser=Firefox&version=16
• Pass parameters from one page to another
• Create a page with a button
• On click of button open second page
• Pass a parameter from first page to second
• Observe the url when you navigate to the Second Page.
Example:
Passing and Retrieving Parameters Between Visualforce Page and Apex Class
• Pass to Page :
Pagereference().getParameters().put()
Public PageReference GoToNewPage() {
PageReference secondPage = Page.mySecondPage;
secondPage.setRedirect(true);
secondPage.getParameters().put('id',account.id);
return secondPage; }
• Retrieve Parameter Values in Apex Class
apexpages.currentpage().getparameters().get()
example:
setRedirect method:
This method Sets the value of the PageReference object's redirect attribute.
If set to true, a redirect is performed through a client side redirect.
If set to false, the redirect is a server-side forward that preserves the view state if and only if the target page uses the same controller and contains the proper subset of extensions used by the source page.
Using Properties Passing and Retrieving Data Between Visualforce Page and Apex Class
<apex:inputText id="num" value="{!Number}" maxlength="4"/>
public with sharing class Add {
Public String Number{get;set;}
….
}
With/Without Sharing:
Use the with sharing or without sharing keywords on a class to specify whether or not to enforce sharing rules on that class.
By default apex code runs into system context. i.e.
- it has access to all objects and fields
- Current user’s credentials are not used to execute the controller logic
- object permissions, field-level security, sharing rules aren’t applied for the current user.
- The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class.
Use the with sharing keyword when declaring a class to enforce the sharing rules that apply to the current user. E.g.
Public with sharing class UpdateCustomer{
………
………
}
Important points to take note:
Sharing settings of the class where the method is defined is applied, not of the class where the method is called.
Eg. if the method is defined in a class with “with sharing” keyword and is called by a class declared with “without sharing”, the method will execute with sharing rules enforced.
If a class isn’t declared as either with or without sharing, the current sharing rules remain in effect.
This means that if a method of this class is called by
- A class declared with “without sharing”, the method will execute with no sharing rules enforced.
- A class declared with “with sharing”, the method will execute with sharing rules enforced.
Both Inner classes and outer classes can be declared as “with sharing”.
- Inner classes do not inherit sharing setting from their container class.
- Inner classes inherit sharing setting from a parent class when one class implements or extends another.
- The sharing setting applies to all code contained in the class including initialization code, constructors and methods.
Sharing and controller extensions:
Like other apex classes, by default, controller extensions run in system mode.
However, if a controller extension extends a standard controller, the logic from the standard controller does not execute in system mode. Instead it executes in user mode, in which, object and field permissions and sharing rules for current user are applied.
You can choose whether to apply or ignore these things for controller extensions my making appropriate use of with or without sharing keywords.
View State:
In general HTTP is a stateless protocol. Therefore there is no native way to implement a "state" in HTTP.
This is done through “View State”
An encrypted, hidden form field that encapsulates the view state of the page.
Only available on Visualforce pages that contain a form component.
Maximum view state allowed size is 135 KB.
Following data types are saved in view state:
•All public and private data members present in Standard, Custom and Controller extensions.
•Objects that are reachable from data members in Custom and Controller extensions.
•The component tree for that page that represents the page’s component structure and the associated state which are the values applied to those components.
•Small amount of data needed by visualforce for other general purposes.
How to check view state?
View State Inspector will be only visible when developer mode is ON in Use
profile.
Maximum view state allowed size is 135 KB.
Following data types are saved in view state:
•All public and private data members present in Standard, Custom and Controller extensions.
•Objects that are reachable from data members in Custom and Controller extensions.
•The component tree for that page that represents the page’s component structure and the associated state which are the values applied to those components.
•Small amount of data needed by visualforce for other general purposes.
How to check view state?
View State Inspector will be only visible when developer mode is ON in Use
profile.
As Viewstate is transferred over http with every response from one page to another page behind the form, it is very necessary to control the size of View State.
There are lots of situations where data is not needed after postback or not needed on Visualforce page then in that case we can make variable or object transient.
The transient variables are not passed to view state and therefore not stored in View State.
There are lots of situations where data is not needed after postback or not needed on Visualforce page then in that case we can make variable or object transient.
The transient variables are not passed to view state and therefore not stored in View State.
0 Comments