复合组件的继承是不可能的.我们为避免代码重复而做的是装饰JSF2复合组件的实现.
我们的应用程序的所有输入字段共享的东西都在装饰器模板中提供,如下所示:
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cu="http://mytags.de/jsftags">
<!-- provides a common set of layout information for inputfields -->
<ui:param name ="fieldStyle" value="#{propertiesController.get('FIELD_STYLE', cc.attrs.name)}" />
<h:panelGroup id="basicInputField" styleClass="basicInputField" layout="block" style="width: #{cc.attrs.width}; height: #{cc.attrs.height};">
<ui:insert name="component">
no component given...
</ui:insert>
</h:panelGroup>
</ui:composition>
Run Code Online (Sandbox Code Playgroud)
复合组件使用模板来装饰自己:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:cc="http://java.sun.com/jsf/composite"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:cu="http://mytags.de/jsftags">
<cc:interface>
<cc:attribute name="name" required="true" />
<cc:attribute name="width" required="false" default="auto" />
<cc:attribute name="height" required="false" default="auto" />
<cc:attribute name="inset" required="false" default="0px" />
</cc:interface>
<cc:implementation>
<ui:decorate template="basicInputField.xhtml">
<ui:define name="component">
<h:inputText id="inputText" style="#{fieldStyle} width: 100%;" value="#{levelContent.test}" />
</ui:define>
</ui:decorate>
</cc:implementation>
</html>
Run Code Online (Sandbox Code Playgroud)
这样我们只需要更改装饰器模板,当我们获取字段属性(即readonly,required,style,...)的方式发生变化时.
| 归档时间: |
|
| 查看次数: |
2291 次 |
| 最近记录: |