扩展一些 JSF 组件并在不丢失属性的情况下添加行为的最佳(但不是最简单)方法之一是使用 PrimeFaces JSF 插件。有关它的一些信息在这里:http ://code.google.com/p/primefaces/wiki/BuildingFromSource 。虽然它有一些硬编码的值(facelets taglib 的名称和输出目录的方式,生成的 taglib 所在的位置)并且可以在本地更改和重建这是 PF jsf 插件的示例
<!-- Primefaces maven plugin -->
<plugin>
<groupId>org.primefaces</groupId>
<artifactId>maven-jsf-plugin</artifactId>
<version>1.2.1-SNAPSHOT</version>
<executions>
<execution>
<id>generate-ui</id>
<phase>generate-sources</phase>
<configuration>
<uri>http://www.mycompany.com/tags</uri>
<name>rstk-tag</name>
<jsfVersion>2</jsfVersion>
<templatesDir>src/main/java-templates</templatesDir>
<componentConfigsDir>src/main/resources-maven-jsf/ui</componentConfigsDir>
<!-- <standardFacesConfig>src/main/resources-maven-jsf/standard-faces-config.xml</standardFacesConfig> -->
<!-- These are new attributes added manually to plugin source code! -->
<standardFaceletsTaglib>src/main/resources-maven-jsf/standard-facelets-taglib.xml</standardFaceletsTaglib>
<faceletsOutputDirectory>target/generated-sources/maven-jsf-plugin/META-INF</faceletsOutputDirectory>
</configuration>
<goals>
<goal>generate-components</goal>
<goal>generate-facelets-taglib</goal>
</goals>
</execution>
</executions>
</plugin>
在此之后,以下元数据 xml 可用于生成:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component SYSTEM "../misc/component.dtd" [
<!ENTITY standard_uicomponent_attributes SYSTEM "../entities/standard_uicomponent_attributes.xml">
<!ENTITY output_component_attributes SYSTEM "../entities/output_component_attributes.xml">
<!ENTITY input_component_attributes SYSTEM "../entities/input_component_attributes.xml">
]>
<component>
<tag>enumSelectOneMenu</tag>
<tagClass>com.rstk.kasko.component.EnumSelectOneMenuTag</tagClass>
<componentClass>com.rstk.kasko.component.EnumSelectOneMenu</componentClass>
<componentType>com.rstk.kasko.component.EnumSelectOneMenu</componentType>
<componentFamily>javax.faces.SelectOne</componentFamily>
<rendererType>javax.faces.Menu</rendererType>
<parent>javax.faces.component.html.HtmlSelectOneMenu</parent>
<description>The tag for select one menu, which renders the enumerations list. No children necessary for this</description>
<attributes>
&input_component_attributes;
</attributes>
</component>
这与 EnumSelectOneMenuTemplate.java(插入到生成的组件代码中的模板)一起允许生成:
- taglib.xml 与所有标准 html 选择一个菜单属性
- 组件类,其中包含用于呈现 clidhren 的自定义逻辑:
public class EnumSelectOneMenu extends HtmlSelectOneMenu {
... // Generated staff here
public boolean getRendersChildren() {
return true;
}
/**
* @see javax.faces.component.UIComponentBase#encodeBegin(javax.faces.context.FacesContext)
*/
@Override
public void encodeBegin(FacesContext context) throws IOException {
super.encodeBegin(context);
if (context.isPostback()) {
return;
}
UISelectItems selectItems = new UISelectItems();
try {
selectItems.setValue(getEnumValuesList());
} catch (Exception e) {
log.error("Failed to create enum list", e);
}
getChildren().add(selectItems);
}
/**
* Creates the list of select items of format [ENUM, enum.getDisplay()]
* @return
*/
private List getEnumValuesList() {
List result = new ArrayList();
ValueExpression ve = getValueExpression("value");
Class enumClass = ve.getType(getFacesContext().getELContext());
Method method = ReflectionUtils.findMethod(enumClass, "getDisplay", null);
for (Object e : ve.getType(getFacesContext().getELContext()).getEnumConstants()) {
result.add(new SelectItem(e, (String) ReflectionUtils.invokeMethod(method, e)));
}
return result;
}
}
然后这个组件可以用作简单的 JSF 选择一个组件(具有所有标准属性),但不需要每次都添加选择项,并且允许将任何其他子项放置在那里:
<s:enumSelectOneMenu value="#{polis.osMatrixType}" id="registryType">
<p:ajax listener="#{osagoPolisBean.rollOsagoEndDate}" update="osagoUsagePeriod" process="osagoTable" event="change"/>
</s:enumSelectOneMenu>