Reputation: 12876
Is it best to NOT label a form component (or any other component for that matter) unless you have a need to do so e.g. if you want to be able to reliably distinguish one component from another?
When I see examples of reference components I am always seeing them with a preceding colon e.g. ":dialog" rather than "dialog" or "form:dialog". Is not naming the form and then specifying the component using a preceding colon the best practice?
Upvotes: 1
Views: 142
Reputation: 1108922
If you don't specify the id
of the form because you don't need to reference it by a client ID right now, then it will technically not harm to omit it, because JSF will autogenerate one in any way.
From inside an ID-less form, for example, you can always reference the parent form as @form
in execute
and render
attributes of <f:ajax>
to signal that you want to submit and/or render the entire parent form.
<f:ajax execute="@form" render="@form" />
But if you need to reference for example another form or one of its child components on ajax render, then you should give that form a fixed ID, so that you will be able to reference it by client ID.
Another reason which we're experiencing in real world projects is that automated web unit testing software such as Selenium really requires a form with a fixed ID, because it needs to find the input fields and submit buttons by their name
attribute which is also composed based on the JSF form ID. If the JSF form ID is not specified, then the input field name is unpredictable on every deploy/request and thus untestable.
All with all, the "best practice" is basically: "only if you need it". I myself am just getting used to always specify the ID of NamingContainer
, UIInput
and UICommand
components. "You never know".
Upvotes: 3
Reputation: 14943
Your component ID would look something like, :namingContainer:myComponent. Where the first “:” tells JSF that you want to start looking for the component at the UIViewRoot instance, or the very top level of the component tree
Read http://ocpsoft.org/java/jsf2-java/how-to-jsf-2-0-render-components-outside-of-the-form/
Upvotes: 1