Labyrinth of Wisdom

-This is My Archive-


【Apex】クラス呼び出し後の画面遷移 (PageReference型の使い方)

クラス呼び出し後の画面遷移の仕方がよくわからなかったので調べてみました。

サンプルコードを使って、PageReference型の簡単な使い方を解説します。

VisualForceページ_Page1

コントローラにJumpTestを指定し、ボタンでjumpメソッドを呼び出します。

<apex:page controller="JumpTest">
  <h1>ここはPage1です</h1>
  <apex:form >
    <apex:commandButton action="{!jump}" value="Page2へジャンプ!" />
  </apex:form>
</apex:page>

Apexクラス_JumpTest

JumpTestクラスの中に、戻り値PageReference型のjumpメソッドがあります。

メソッドの最後にreturnでVisualForceページPage2を返します。 ここで指定した画面に遷移します。

因みにreturn nullだと元の画面に遷移します。(今回で言うとPage1)

public class JumpTest(){
  public PageReference jump(){
    return Page.Page2;
  }
}

VisualForceページ_Page2

<apex:page>
  <h1>ジャンプ成功!ここはPage2です!</h1>
</apex:page>