In this recipe, we will demonstrate how to use JSF ID Generator. This is an Eclipse plug-in that generates customizable and unique component IDs for JSF tags. It is very useful when you write large JSF pages, and you are sick and tired of manually specifying the id
attribute.
First, you have to download the JSF ID Generator plug-in from the address http://sourceforge.net/projects/jsfidgenerator/.
We start by installing the new plug-in in Eclipse. For this, follow the given steps:
/links
inside the eclipse
folder./links
folder and name it as say jsf.link
(notice that you can provide any other name, only the extension is mandatory).C:PacktJSFKitID
, the contents of jsf.link
has to be this:path=C:PacktJSFKitID
/eclipse
folder, which in turn has /features
and /plugins
as subfolders. For example, in this case the /ID
folder has a subfolder named /eclipse
, which has two subfolders, named /features
and /plugins
. In the /plugins
folder you should paste the JSF ID Generator JAR file.*.link
files.If you don't give an ID to a JSF component, then JSF ID Generator generates one at runtime with a prefix such as j_id_jsp_
. You can modify this prefix directly from Eclipse, by following these steps:
Now, you can start to create a JSF application under Eclipse to test the JSF ID Generator plug-in. Assume that you have integrated a JSF form on a JSF page (name it formids.jsp
) as shown next (notice that we have intentionally omitted the id
attribute for each component):
… <h:form> <h:outputText value="Enter Your Name:" /> <h:inputText value="Somethging from a bean!" /> <h:commandButton action="some_page.jsp" value="OK" /> </h:form> …
Next, in the Package View of your project, right-click on the JSF page and select JSF ID Generator | Generate JSF IDs from the contextual menu, as shown in the following:
When JSF ID Generator has finished its job, you can see a generated document under the JSF page named formids.jsp.bak
(this is just a backup of the initial page) and more importantly our form now has generated IDs:
… <h:form id="id0"> <h:outputText id="id1" value="Enter Your Name:" /> <h:inputText id="id2" value="Somethging from a bean!" /> <h:commandButton id="id3" action="some_page.jsp" value="OK" /> </h:form> …
Notice that the generated IDs respect the specified ID prefix.