<apex:include> を使用する既存ページの参照
何も変更を行わずに他のページのコンテンツ全体を複製する場合は、<apex:include> タグを使用します。この技法を使用すると、複数の場所で同じように使用する既存のマークアップを参照することができます。
たとえば、ユーザの名前を取得して表示するフォームを作成するとします。最初に、formTemplate というページを作成して、再利用可能なフォームを表し、templateExample というコントローラを使用します。
1<apex:page controller="templateExample">
2
3</apex:page>
templateExample が存在しないというメッセージが表示されたら、次のコードを使用してカスタムコントローラを定義します。
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class templateExample{
18
19 String name;
20 Boolean showGreeting = false;
21
22 public PageReference save() {
23 showGreeting = true;
24 return null;
25 }
26
27 public void setNameField(String nameField) {
28 name = nameField;
29 }
30
31 public String getNameField() {
32 return name;
33 }
34
35 public Boolean getShowGreeting() {
36 return showGreeting;
37 }
38}次に、formTemplate に戻り、次のマークアップを追加します。
[保存] をクリックしても何も起こりません。これは予期される動作です。
1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="templateExample">
18 <apex:form>
19 <apex:outputLabel value="Enter your name: " for="nameField"/>
20 <apex:inputText id="nameField" value="{!nameField}"/>
21 <apex:commandButton action="{!save}" value="Save" id="saveButton"/>
22 </apex:form>
23</apex:page>次に、formTemplate を含む displayName というページを作成します。
1swfobject.registerObject("clippy.codeblock-3", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="templateExample">
18 <apex:include pageName="formTemplate"/>
19 <apex:actionSupport event="onClick"
20 action="{!save}"
21 rerender="greeting"/>
22 <apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}"/>
23</apex:page>このページを保存すると、formTemplate ページ全体がインポートされます。名前を入力し、[保存] をクリックすると、フォームから true 値が showGreeting 項目に��されて、<apex:outputText> とユーザ名が表示されます。
別の Visualforce ページを作成し、ページで formTemplate を使用して異なる挨拶文を表示することもできます。displayBoldName というページを作成し、次のマークアップを使用します。
表示されるテキストが変更されても、templateExample ロジックは同じままです。
1swfobject.registerObject("clippy.codeblock-4", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17<apex:page controller="templateExample">
18 <style type="text/css">
19 .boldify { font-weight: bolder; }
20 </style>
21 <apex:include pageName="formTemplate"/>
22 <apex:actionSupport event="onClick"
23 action="{!save}"
24 rerender="greeting"/>
25 <apex:outputText id="greeting" rendered="{!showGreeting}"
26 styleClass="boldify"
27 value="I hope you are well, {!nameField}."/>
28</apex:page>