As already mentioned, HTML name and identifier attributes are generated by naming containers and based on the application namespace. This prevents collisions when the controls are children of repeating controls (for example, UIData ), or the JSP is displayed twice on the same page (for example, in a portlet environment). The identifier displayed in HTML is clientId .
You can hardcode or build clientId manually, but this is a very fragile approach. Better use the getClientId component (FacesContext); this is what renderers use.
A bean that can get the clientId for the associated component:
public class IdBean implements Serializable { private UIComponent mytext; public String getClientId() { return mytext.getClientId(FacesContext.getCurrentInstance()); } public UIComponent getMytext() { return mytext; } public void setMytext(UIComponent mytext) { this.mytext = mytext; } public List<String> getRows() { List<String> rows = new ArrayList<String>(); for (int i = 0; i < 10; i++) { rows.add("row" + i); } return rows; } }
View:
<f:view> <h:form> <h:dataTable value="#{idBean.rows}" var="row"> <h:column> <h:outputLabel value="#{row}" /> <h:inputText binding="#{idBean.mytext}" onclick="foo('#{idBean.clientId}');" /> </h:column> </h:dataTable> </h:form> </f:view> <script type="text/javascript"> function foo(name) { alert('You clicked '+name); } </script>
The mytext control is displayed 10 times, so any code that emits its name must also be a child of the dataTable.
source share