问题
Maybe this is a dumb question, but I use
cc
to refer to the composite component, for instance cc.attrs.randomAttr but I have also seen the
component
implicit object and I have used it because I was told to but I don't really understand what it is for. Can anyone explain please?
回答1:
cc
refers to the top level composite component that is being processed at the time of evaluation.
component
simply is the ui component being processed.
So when inside a composite component, cc
refers to the 'parent' component, while component
when used on an individual component refers to that particular instance. Or for simple cases:
cc
== component.getCompositeComponentParent(component)
, with component being a component of which the composite component is build.
E.g. consider the following composite component:
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:cc="http://java.sun.com/jsf/composite"
>
<cc:interface/>
<cc:implementation>
<h:outputText value="Own ID: #{component.id}, parent composite ID: #{cc.id}" /> <br/>
<h:outputText value="Own ID: #{component.id}, parent composite ID: #{cc.id}" />
</cc:implementation>
</html>
Using this on a Facelet will print 2 different "own" IDs, namely the ones of the two outputText components, while the composite ID will be the same on both lines.
Note that things may become a little more complicated when multiple nestings of composite components are involved.
来源:https://stackoverflow.com/questions/5124961/jsf-difference-between-implicit-objects-cc-and-component