[OmniFaces utilities] The
getClosestParent()
returns from the given component the closest parent of the given parent type, or null
if none is found.Method:
Usage:
JSF Page
Snippet (we will search the closest parent of a certain type for the
highlighted components (uiInput, uiSelectItem and uiParameter)
<h:body>
...
<h:form id="myFormId">
<h:inputText id="nameId"
value="#{playersBean.name}"/>
<h:inputText id="surnameId"
value="#{playersBean.surname}"/>
<h:inputText id="ageId"
value="#{playersBean.age}"/> <!-- this is 'uiInput'-->
<h:selectOneMenu id="selectId"
value="#{playersBean.title}">
<f:selectItem id="rgItemId" itemValue="Roland
Garros" itemLabel="Roland Garros" />
<f:selectItem id="uoItemId" itemValue="US Open"
itemLabel="US Open" />
<f:selectItem id="aoItemId" itemValue="Australian
Open" itemLabel="Australian Open"/> <!-- this is 'uiSelectItem'-->
<f:selectItem id="wItemId" itemValue="Wimbledon"
itemLabel="Wimbledon" />
</h:selectOneMenu>
<h:commandButton id="btnId"
value="Save" action="#{playersBean.save()}">
<f:param id="auParamId" name="titles"
value="Australian Open"/>
<f:param id="rgParamId" name="titles"
value="Roland Garros"/> <!-- this is 'uiParameter'-->
<f:param id="wParamId"
name="titles" value="Wimbledon"/>
<f:param id="uoParamId"
name="titles" value="US Open"/>
</h:commandButton>
</h:form>
...
</h:body>
Now, let's
see some examples (the output contains the closest parent class name and clientId):
·
find the closest parent of type UIForm
for 'uiInput'
UIForm
closestUIFormParent = Components.getClosestParent(uiInput, UIForm.class);
Output:
Closest form
for 'uiInput': Class: class javax.faces.component.html.HtmlForm ClientId: myFormId
·
find the closest parent of type UIForm
for 'uiSelectItem'
(obviously, the same form as above)
UIForm
closestUIFormParent = Components.getClosestParent(uiSelectItem, UIForm.class);
Output:
Closest form
for 'uiSelectItem': Class: class javax.faces.component.html.HtmlForm ClientId: myFormId
·
find the closest parent of type UIForm
for ' uiParameter'
(obviously, the same form as above)
UIForm
closestUIFormParent = Components.getClosestParent(uiParameter, UIForm.class);
Output:
Closest form
for 'uiParameter': Class: class javax.faces.component.html.HtmlForm ClientId: myFormId
·
find the closest parent of type UIOutput
for ' uiInput'
UIOutput
closestUIOutputParent = Components.getClosestParent(uiInput, UIOutput.class);
Output:
Closest
UIOutput for 'uiInput': Class: class javax.faces.component.html.HtmlBody ClientId: j_idt7
·
find the closest parent of type UICommand
for ' uiParameter'
UICommand
closestUICommandParent = Components.getClosestParent(uiParameter,
UICommand.class);
Output:
Closest
UICommand for 'uiParameter': Class: class
javax.faces.component.html.HtmlCommandButton
ClientId: myFormId:btnId
Note If
the closest parent cannot be obtained, this method returns null.
Niciun comentariu :
Trimiteți un comentariu