How does JSF generate a form input field name?

Somebody knows? Is it possible that we will indicate the name of the form input field? How to do it?

+4
source share
4 answers

In general, all JSF components have an identifier. If you do not provide it, JSF will create an automatic identifier using the j _ idXXX format (XXX is an increased number).

Some components implement the javax.faces.component.NamingContainer interface, in particular <h:form> . This means that all children of this component will have their ID with the identifier prefix of this container, separated by the ":" symbol. So in the example:

 <h:form id="myForm"> <h:inputText id="myInput" .../> </h:form> 

the actual input identifier (i.e., the identifier of the input HTML object) will be myForm: myInput .

+5
source

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:

 /** Request scope */ 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.

+5
source

It is generated as formId: fieldId

So, if you had the following:

 <h:form id="searchForm"> <h:inputText id="searchField" ... /> </h:form> 

The name (and HTML identifier) โ€‹โ€‹of the search field will be:

searchForm: searchField

+3
source

For most pages containing a presentation, form, and some components, the client identifier will be a colon-separated one containing the form identifier and component identifier. Example:

The input identifier identifier will be "myForm: myInputText". If you are nested in a preview, then this will be the first in the list, for example:

Now the identifier of the input text client will be "mySubview: myForm: myInputText".

0
source

All Articles