Find component

Hi everyone,

I needto find a component by its id in the view from the code in the bean. I have written the following method, but is working only when the way is straight, i mean

view

html

head

body

form

component

component_to_be_found

but not in the following case, or alikes:

view

html

head

body

form

component 1

component 1.1

component 1.2

component_to_be_found

component 2

the method is as follows:

public UIComponent getElementById(UIComponent base, String compId) {

Dummy.println("Base = " + base);

if (compId.equals(base.getId())) {

Dummy.println("Encontrado = " + base);

return base;

}

UIComponent component = null;

ArrayList<UIComponent> list = (ArrayList<UIComponent>) base.getChildren();

for (UIComponent component2 : list) {

if ((component = getElementById(component2, compId)) != null) {

return component;

}

}

return null;

}

any help is mostly welcome,

ip

[1117 byte] By [iapazminoa] at [2007-11-27 5:02:48]
# 1
You simply also have to iterate through it's children. But isn't UIViewRoot#findComponent() sufficient for you?UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent(componentId);
BalusCa at 2007-7-12 10:20:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

I dont know why using just

UIComponent component = FacesContext.getCurrentInstance().getViewRoot().findComponent(componentId);

doesn't work. maybe because i have there are fragmetns included in thhe jsp.

any way, the same method is working just fine now, but i have to make sure, fragments containing the desired id are not the last thing in the page so they are instantiated before the search begins

thanks.

iapazminoa at 2007-7-12 10:20:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...