0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Visualforce_画面遷移で値を渡す方法(POST)

Last updated at Posted at 2025-01-01

VisualforceページからVisualforceページへPOSTで値を渡す一般的な方法だよ!
まず、完成形サンプルコード!

FirstPage.page
<apex:page controller="FirstPageController" showHeader="true" sidebar="true">
    <!-- ここでは apex:form ではなく、純粋な HTML の form を使う -->
    <form action="/apex/SecondPage" method="POST">
        <!-- 非表示項目(隠しフィールド) -->
        <input type="hidden" name="passValue" value="{!passValue}" />
        
        <!-- 送信ボタン -->
        <input type="submit" value="次のページへ"/>
    </form>
    ・FirstpageからPOSTで渡す隠し文字列:{!passValue}<br/>
    (分かりやすく表示)
</apex:page>
FirstPageController.cls
public with sharing class FirstPageController {
    // 送信したい値を保持するプロパティ
    public String passValue { get; set; }
    
    // 今回はコンストラクタで値をセットしておきます
    public FirstPageController() {
        // ここでは例として固定文字列をセット
        passValue = 'Hidden Text';
    }
}
SecondPage.page
<apex:page controller="SecondPageController" showHeader="true" sidebar="true">
    <h1>SecondPage</h1>
    <p>Firstpageから受け取った値: {!receiveValue}</p>
</apex:page>
SecondPageController.cls
public with sharing class SecondPageController {
    // 受け取る値を表示するためのプロパティ
    public String receiveValue { get; set; }
    
    public SecondPageController() {
        // POSTされてきたフォームのパラメータを取得
        receiveValue = ApexPages.currentPage().getParameters().get('passValue');
    }
}

実装手順(1から順番に作成してください)

1. Apexクラス:FirstPageController
2. Apexクラス:SecondPageController
3. Visualforceページ:SecondPage
4. Visualforceページ:FirstPage

各ページ、コントローラの役割

FirstPage:
遷移元となるVisualforceページ。
ここでユーザが「次のページへ」ボタンを押すと、ある値をPOSTで送信します。

SecondPage:
遷移先のVisualforceページ。
FirstPageからPOSTで送られた値を受け取り、画面に表示します。

Controller:
値の持ち運びや受け取りを行うApexクラス。
FirstPageController(FirstPage用)
SecondPageController(SecondPage用)

コードの流れ

1.ユーザがFirstPageへアクセス
ブラウザでFirstPageが読み込まれる。
FirstPageのController(FirstPageController)が呼び出され、passValue というプロパティに “Hidden Text” が設定される。
その値が apex:inputHidden にバインドされ、画面には表示されないが、裏側ではフォーム内に保持されている。

2.「次のページへ」ボタン押下 (フォームのPOST送信)
ユーザがクリックすると、form が POSTリクエスト を行い、SecondPage (/apex/SecondPage) へデータを飛ばす。
送信内容 は画面にあるフォーム要素(今回は apex:inputHidden のみ)が含まれる。
そしてSecondPageへのリクエスト到着。

3.SalesforceサーバがSecondPageの処理を受け取り、SecondPageController が呼び出される
SecondPageController のコンストラクタ内などApexPages.currentPage().getParameters() を通じて、POSTで送られてきたパラメータを取得。

4.SecondPageの表示
取得した値(receiveValue) を Visualforceの {!receiveValue} で画面に出力
これにより、FirstPageで設定された文字列がSecondPageの画面に表示される。




URLパラメータに付与して渡す方法もあるけど、クラッキングのリスクもあるのでPOSTで渡す方法を今回は記載したよ。
ヒントになれば幸いです!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?