Visualforce 提供了多种策略,用于在 多个 Visualforce 页面。您选择的方法取决于您需要的灵活性 重用模板。模板方法越灵活, 此外,还可以修改使用该方法的任何模板实现。 可以使用以下模板方法,按大多数顺序排列 最不灵活:定义自定义组件类似于封装一件作品的方式 代码,然后在 程序中,您可以将通用设计模式封装在自定义组件中 ,然后在一个或多个 Visualforce 页面中多次重复使用该组件。定义自定义组件是最灵活的模板 方法,因为它们可以包含任何有效的 Visualforce 标签,并且可以不受限制地导入到任何 Visualforce 页面中。但是,不应使用自定义组件来定义可重用的 Visualforce 页面。如果要重复使用整个 Visualforce 页面的内容,请选择其他两种模板方法之一。使用 <apex:composition> 定义模板如果要定义一个基本模板,该模板允许 要随每个实现而更改的模板,请使用组件。 此模板方法最适合需要维护的情况 一个页面的整体结构,但需要单个内容 页面要不同,比如一个网站的新闻文章不同 文章应以相同的页面布局显示。<apex:composition>通过这种技术,您还可以从 控制器返回的 PageReference。使用 <apex:include> 引用现有页面如果要将 Visualforce 页面的全部内容插入到另一个页面中,请使用该组件。这 模板方法最适合想要复制的情况 多个区域中的相同内容,例如显示的反馈表单 在网站的每个页面上。<apex:include>
仅当您想要引用已存在的 Visualforce 页面时,才应使用使用模板。如果只需要复制一组组件, 使用自定义组件。<apex:insert><apex:composition>
使用 <apex:composition> 定义模板
使用定义的所有模板都必须具有一个或多个子标记。标记指示导入模板的页面,该部分需要 一个定义。任何导入模板的 Visualforce 页面都必须用于指定模板每个部分的内容。<apex:composition><apex:insert><apex:insert><apex:composition><apex:define><apex:insert>
您可以创建一个框架模板,该模板允许后续 Visualforce 页面在同一标准结构中实现不同的内容。 为此,请使用该标记创建模板页面。<apex:composition>
下面的示例演示如何使用 、 和 实现框架 模板。<apex:composition><apex:insert><apex:define>首先,创建一个名为 的空页面,该页面使用名为
myFormCompositioncompositionExample
<apex:page controller="compositionExample">
</apex:page>
保存页面后,会出现一个提示,要求您创建 .使用以下命令 用于定义该自定义控制器的代码:
compositionExample
public class compositionExample{
String name;
Integer age;
String meal;
String color;
Boolean showGreeting = false;
public PageReference save() {
showGreeting = true;
return null;
}
public void setNameField(String nameField) {
name = nameField;
}
public String getNameField() {
return name;
}
public void setAgeField(Integer ageField) {
age= ageField;
}
public Integer getAgeField() {
return age;
}
public void setMealField(String mealField) {
meal= mealField;
}
public String getMealField() {
return meal;
}
public void setColorField(String colorField) {
color = colorField;
}
public String getColorField() {
return color;
}
public Boolean getShowGreeting() {
return showGreeting;
}
}
接下来,返回并创建骨架模板:
myFormComposition
<apex:page controller="compositionExample">
<apex:form >
<apex:outputLabel value="Enter your name: " for="nameField"/>
<apex:inputText id="nameField" value="{!nameField}"/>
<br />
<apex:insert name="age" />
<br />
<apex:insert name="meal" />
<br />
<p>That's everything, right?</p>
<apex:commandButton action="{!save}" value="Save" id="saveButton"/>
</apex:form>
</apex:page>
请注意,两个字段需要 和内容。这 这些字段的标记是在调用此合成的页面中定义的 模板。
<apex:insert>agemeal接下来,创建一个名为 的页面,该页面 定义 中的标签:
myFullForm<apex:insert>myFormComposition
<apex:page controller="compositionExample">
<apex:messages/>
<apex:composition template="myFormComposition">
<apex:define name="meal">
<apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
<apex:inputText id="mealField" value="{!mealField}"/>
</apex:define>
<apex:define name="age">
<apex:outputLabel value="Enter your age: " for="ageField"/>
<apex:inputText id="ageField" value="{!ageField}"/>
</apex:define>
<apex:outputLabel value="Enter your favorite color: " for="colorField"/>
<apex:inputText id="colorField" value="{!colorField}"/>
</apex:composition>
<apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}.
You look {!ageField} years old. Would you like some {!colorField} {!mealField}?"/>
</apex:page>
请注意有关标记的以下信息:
- 保存时,将显示先前定义的标记和保存按钮。myFullForm<apex:inputText>
- 由于组合页面需要 和 字段,因此定义它们 作为文本输入字段。它们在页面上的显示顺序确实如此 没关系; 指定该字段始终显示在该字段之前。agemealmyFullFormmyFormCompositionagemeal
- 即使没有,该字段仍会导入 匹配字段。name<apex:define>
- 即使该字段被忽略,即使 该字段存在控制器代码。这是因为成分 模板不需要任何名为 的字段。colorcolor
- 和字段 不需要是文本输入。标记中的组件可以是任何有效的 Visualforce 标记。agemeal<apex:define>
要展示如何在标签中使用任何有效的 Visualforce,请执行以下操作: 创建一个新的 Visualforce 页面,该页面名为 并使用以下标记:
<apex:define>myAgelessForm
<apex:page controller="compositionExample">
<apex:messages/>
<apex:composition template="myFormComposition">
<apex:define name="meal">
<apex:outputLabel value="Enter your favorite meal: " for="mealField"/>
<apex:inputText id="mealField" value="{!mealField}"/>
</apex:define>
<apex:define name="age">
<p>You look great for your age!</p>
</apex:define>
</apex:composition>
<apex:outputText id="greeting" rendered="{!showGreeting}" value="Hello {!nameField}.
Would you like some delicious {!mealField}?"/>
</apex:page>
请注意,只有合成模板 需要标记才能存在。在此示例中,定义 作为文本。
<apex:define>age
动态模板
动态模板允许您通过 PageReference 分配模板。 模板名称分配给返回的控制器方法 包含要使用的模板的 PageReference。为 示例,创建一个名为“定义框架模板”的页面:
myAppliedTemplate
<apex:page>
<apex:insert name="name" />
</apex:page>
接下来,创建一个名为 将返回对此页面的引用的方法:
public class dynamicComposition {
public PageReference getmyTemplate() {
return Page.myAppliedTemplate;
}
}
最后,创建一个名为实现 此控制器和动态模板:
myDynamicComposition
<apex:page controller="dynamicComposition">
<apex:composition template="{!myTemplate}">
<apex:define name="name">
Hello {!$User.FirstName}, you look quite well.
</apex:define>
</apex:composition>
</apex:page>