This is for academic purposes
I am trying to run some code from visualforce in practise and came across this sample page and related controller
page is
<apex:page controller=”myController” >
<apex:form >
<apex:pageBlock title=”My Content” mode=”edit”>
<apex:pageBlockButtons >
<apex:commandButton action=”{!save}” value=”Save”/>
</apex:pageBlockButtons>
<apex:pageBlockSection title=”My Content Section”
columns=”2”>
<apex:outputLabel for=”aName”>Account Name:</
apex:outputLabel>
<apex:inputText value=”{!accountName}”/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
controller code is
public with sharing class myController {
private final Id accountId ;
transient public final String accountName {get; set; }
public myController() {
Account account = [select Id, Name from Account where id
=:ApexPages.currentPage().getParameters().get(‘id’)];
accountId = account.Id ;
accountName = account.Name ;
}
public PageReference save() {
Account myAccount = [select name from Account where id
=:accountId];
myAccount.name = accountName;
update myAccount;
return null;
}
}
where i am confused is,
myAccount.name = accountName;
This code works like this:
Page gets pre populated with account name on load.User enters edits account name.Clicks Save.It gets saved to DB.
Now this is where i am confused:
We marked 'account name' variable as transient in controller.So how come value entered by user still persist in 'accountname' variable. When user clicks save,save method is executed during the post back request.since accountname is not view state i thought variable will not have the value entered by user
Maybe i don't understand view state properly. Can somebody clarify this please?