2011-06-14 75 views
7

JSF 2複合組件中是否存在繼承關係?JSF 2複合組件中是否存在繼承關係?

據我所知,沒有。 我只是確認。

謝謝!

+0

我不確定要給出準確的答案,但我認爲在複合組件中,繼承的替代是子組件。參見:http://weblogs.java.net/blog/cayhorstmann/archive/2010/01/30/composite-input-components-jsf – sfrj 2011-10-20 20:06:14

回答

5

複合組件的繼承是不可能的。我們爲避免代碼重複所做的工作是修飾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> 

和複合組件使用模板來裝點自己:

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

這樣我們只需要改變裝飾模板,當我們獲取字段屬性的方式(即只讀,必需,樣式等)發生變化時。