複数Visualforceページ間で値の引き継ぎ
Visualforceでページを遷移する際に値を引き継ぐ2つの方法をサンプルコードベースで説明します。
ビューステート
複数のVFで同一のコントローラを使用することで、PageReferenceにより遷移するとビューステートの項目を引き継ぐことができます。
サンプルコードでは、TransferController.clsのconfirmメソッドでTest_TransferToState.pageのPageReferenceを返して遷移している部分になります。
Getパラメータ
複数のVFでコントローラを分割する場合には、URLのGetパラメータを使用することで項目を引き継ぐことができます。
サンプルコードでは、TransferController.clsのnextメソッドでTest_TransferToParam.pageのPageReferenceにpr.getParameters().put(”,”)で値を設定し、遷移して受けるVFのコントローラ(TransferReceiveController.cls)でApexPages.currentPage().getParameters().get(”)により値を取得します。
サンプルコード
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TransferController { | |
public String test1 {set; get;} | |
public String test2 {set; get;} | |
public String test3 {set; get;} | |
public String test4 {set; get;} | |
public TransferController(){ | |
this.test1 = 'Hello'; | |
this.test2 = 'World'; | |
this.test3 = 'Hello2'; | |
this.test4 = 'World2'; | |
} | |
public PageReference confirm(){ | |
// PageReferenceで遷移 | |
return Page.Test_TransferToState; | |
} | |
public PageReference next(){ | |
PageReference pr = Page.Test_TransferToParam; | |
pr.getParameters().put('test3', this.test3); | |
pr.getParameters().put('test4', this.test4); | |
return pr; | |
} | |
public PageReference returnPage(){ | |
return Page.Test_TransferFrom; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TransferReceiveController { | |
public String test3 {set; get;} | |
public String test4 {set; get;} | |
public TransferReceiveController(){ | |
this.test3 = ApexPages.currentPage().getParameters().get('test3'); | |
this.test4 = ApexPages.currentPage().getParameters().get('test4'); | |
} | |
public PageReference returnPage(){ | |
return Page.Test_TransferFrom; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="TransferController"> | |
<apex:form id="form"> | |
<head> | |
</head> | |
<body> | |
<apex:pageBlock title="My Content" mode="edit"> | |
<apex:pageBlockButtons > | |
<apex:commandButton action="{!confirm}" value="Confirm"/> | |
<apex:commandButton action="{!next}" value="Next"/> | |
</apex:pageBlockButtons> | |
<apex:pageBlockSection title="My Content Section" columns="2"> | |
<apex:inputText value="{!test1}"/> | |
<apex:inputText value="{!test2}"/> | |
<apex:inputText value="{!test3}"/> | |
<apex:inputText value="{!test4}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</body> | |
</apex:form> | |
</apex:page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="TransferController"> | |
<apex:form id="form"> | |
<head> | |
</head> | |
<body> | |
<apex:pageBlock title="My Content" mode="detail "> | |
<apex:pageBlockButtons > | |
<apex:commandButton action="{!returnPage}" value="Return"/> | |
</apex:pageBlockButtons> | |
<apex:pageBlockSection title="My Content Section State" columns="2"> | |
<apex:outputText value="{!test1}"/> | |
<apex:outputText value="{!test2}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</body> | |
</apex:form> | |
</apex:page> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="TransferReceiveController"> | |
<apex:form id="form"> | |
<head> | |
</head> | |
<body> | |
<apex:pageBlock title="My Content" mode="detail "> | |
<apex:pageBlockButtons > | |
<apex:commandButton action="{!returnPage}" value="Return"/> | |
</apex:pageBlockButtons> | |
<apex:pageBlockSection title="My Content Section Param" columns="2"> | |
<apex:outputText value="{!test3}"/> | |
<apex:outputText value="{!test4}"/> | |
</apex:pageBlockSection> | |
</apex:pageBlock> | |
</body> | |
</apex:form> | |
</apex:page> |
補足
こちらにもアップしてます。
https://github.com/yhayashi30/visualforce-transfer-sample.git