Skip to content

Instantly share code, notes, and snippets.

@martyychang
Created December 22, 2014 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martyychang/9a2408ddf3b5a837b755 to your computer and use it in GitHub Desktop.
Save martyychang/9a2408ddf3b5a837b755 to your computer and use it in GitHub Desktop.
Where's the Visualforce viewstate coming from? How is it growing from Exhibit 5 to Exhibit 6?
<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<h1>
Exhibit 1: No controller, no form
</h1>
<p>
This Visualforce page uses no controller, standard or custom,
and it also has no form on the page.
</p>
</apex:page>
<apex:page sidebar="false" showHeader="false" standardStylesheets="false"
standardController="Lead">
<h1>
Exhibit 2: Standard Lead controller, no form
</h1>
<p>
This page uses <code>standardController="Lead"</code>,
but there is no <code>apex:form</code> on the page.
</p>
</apex:page>
<apex:page sidebar="false" showHeader="false" standardStylesheets="false"
controller="PublicViewstateExhibit3Controller">
<h1>
Exhibit 3: Custom controller with persistent property, no form
</h1>
<p>
This controller has a property defined as follows:
<code>public Lead record { get; set; }</code>
</p>
</apex:page>
public class PublicViewstateExhibit3Controller {
public Lead record { get; set; }
}
<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<h1>
Exhibit 4: No controller, one form
</h1>
<apex:form>
<p>
This paragraph is enclosed in an <code>apex:form</code>
on a page that uses no controller, standard or custom.
</p>
</apex:form>
</apex:page>
<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<h1>
Exhibit 5: No controller, two forms
</h1>
<apex:form>
<p>
This paragraph is enclosed in an <code>apex:form</code>
on a page that uses no controller, standard or custom.
</p>
</apex:form>
<p>
This second paragraph is <em>not</em> enclosed in an <code>apex:form</code>.
</p>
<apex:form>
<p>
This third paragraph is enclosed in a different <code>apex:form</code>
on the same page.
</p>
</apex:form>
</apex:page>
<apex:page sidebar="false" showHeader="false" standardStylesheets="false">
<h1>
Exhibit 6: No controller, two forms
</h1>
<apex:form>
<p>
This paragraph is enclosed in an <code>apex:form</code>
on a page that uses no controller, standard or custom.
</p>
</apex:form>
<p>
This second paragraph is <em>not</em> enclosed in an <code>apex:form</code>.
</p>
<apex:form>
<p>
This third paragraph is enclosed in a different <code>apex:form</code>
on the same page.
</p>
</apex:form>
<p>
If you compare this to
<apex:outputLink value="{!URLFOR($Page.PublicViewstateExhibit5)}">Exhibit 5</apex:outputLink>,
even though everything else on this page is identical except
for this extra <code>p</code> element, the viewstate actually
has increased in size!
</p>
</apex:page>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment