You can use UIComponent#findComponent()
for this. It will be searched relative to the naming container parent. So if you can guarantee that those components have an unique naming container parent (e.g. <ui:repeat>
, <f:subview>
, <h:form>
, etc), then do so:
<h:someInput id="someInput" ... />
<h:someOutput ... rendered="#{component.findComponent('someInput').valid}" />
As to binding
, you should just make sure that the value of the binding
attribute is exclusively tied to the component itself, and not shared across multiple components.
So, this is wrong when it concerns a component in a reusable include/tagfile/composite:
<h:someInput binding="#{someInput}" ... />
<h:someOutput ... rendered="#{someInput.valid}" />
Rather bind it to an unique key. Let the include/tagfile/composite require a id
param/attribute and then use <c:set>
to create a variable which appends the id
so that you can ultimately use it as key of request scope map.
<c:set var="binding" value="binding_someInput_#{id}" />
<h:someInput id="#{id}" binding="#{requestScope[binding]}" ... />
<h:someOutput ... rendered="#{requestScope[binding].valid}" />
To keep the request scope clean, consider creating a hash map in request scope via faces-config.xml
:
<managed-bean>
<description>Holder of all component bindings.</description>
<managed-bean-name>components</managed-bean-name>
<managed-bean-class>java.util.HashMap</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<h:someInput id="#{id}" binding="#{components[id]}" ... />
<h:someOutput ... rendered="#{components[id].valid}" />
In case of composite components, there's another way. Bind it to the backing component.
<cc:interface componentType="someComposite">
...
</cc:interface>
<cc:implementation>
<h:someInput binding="#{cc.someInput}" ... />
<h:someOutput ... rendered="#{cc.someInput.valid}" />
</cc:implementation>
@FacesComponent("someComposite")
public class SomeComposite extends UINamingContainer {
private UIInput someInput; // +getter+setter
// ...
}
In a decently designed composite you often already have or ultimately need it anyway.