I want to find some UIComponent
from managed bean by the id that I have provided.
I have written the following code:
private UIComponent getUIComponent(String id) {
return FacesContext.getCurrentInstance().getViewRoot().findComponent(id) ;
}
I have defined a p:inputTextarea
as:
<p:inputTextarea id="activityDescription" value="#{adminController.activityDTO.activityDescription}" required="true" maxlength="120"
autoResize="true" counter="counter" counterTemplate="{0} characters remaining." cols="80" rows="2" />
Now if a call to the method as getUIComponent("activityDescription")
it is returning null
, but if I call it as getUIComponent("adminTabView:activityForm:activityDescription")
then I can get the org.primefaces.component.inputtextarea.InputTextarea
instance.
Is there any way to get the component with only the id i.e., "activityDescription" not the absolute id i.e., "adminTabView:activityForm:activityDescription"?
You can use the following code:
This code will find only the first component in the tree with the
id
you pass. You will have to do something custom if there are 2 components with the same name in the tree (this is possible if they are under 2 different naming containers).Maybe it's not possible. The
FacesContext.getCurrentInstance().getViewRoot().findComponent(id)
method returns only oneUIComponent
. The ViewRoot is constructed as a tree so if you have two forms in the view, each one with a component withid="text"
, they will have it's parent components added to the id so they won't conflict. If you put the twoid="text"
components within the same form, you will havejava.lang.IllegalStateException
thrown.If you want to find all components with the searched id, you could write a method that implements:
Or if you want to find the first occurrence:
Yes, in all parent components which are
NamingContainers
you have to add attributeprependId="false"
- it will works in<h:form>
for sure and should work in others.If it is not possible to set it via attribute in .xhtml file you have to set such value programaticaly.
Suggestion after question's author comment:
If there is not such attribute in components that you are using try write find method like this:
Next just invoke
That will help?
I try this code, and it's help:
Thanks
Just put
prependId="false"
to your form in which this textarea is.