开发者

Is there such a thing as inheritance in JSF 2 composite components?

Is there such a thing as inheritance in JSF 2 composite components?

As far as I开发者_StackOverflow社区 know, there isn't. I'm just making sure.

Thanks!


Inheritance of composite components is not possible afaik. What we did to avoid code duplication is to decorate the implementation of a JSF2 composite component.

The stuff shared by all input fields of our application is provided within a decorator template like this:

<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>

And the composite component uses the template to decorate itself:

<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>

This way we only need to change the decorator template, when the way we fetch field properties (i.e. readonly, required, style,...) changes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜