Using JSF ID Generator

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.

Getting ready

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:

  1. Create a folder named /links inside the eclipse folder.
  2. Create a new file inside the /links folder and name it as say jsf.link (notice that you can provide any other name, only the extension is mandatory).
  3. Assuming that we have copied the JSF ID Generator into C:PacktJSFKitID, the contents of jsf.link has to be this:
    path=C:PacktJSFKitID
    
  4. The path should point to a directory that has a /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.
  5. Restart Eclipse and now you should be able to read all the plugins and feature descriptions from the path referred to by the *.link files.

    Note

    In the libraries bundled with this book you have a /ID folder under the /JSF_lib/JSF ID Generator folder, which is all you need for this recipe.

How to do it...

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:

  1. Launch the Preferences window from the Window main menu.
  2. In the left tree, locate and select the JSF ID Generator entry.
  3. In the right panel, insert the desired prefix in the ID Prefix text field, as shown in the following figure.
  4. Click Apply and Ok buttons.
    How to do it...

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:

How to do it...

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.

How it works...

After the page backup is created the JSF ID Generator identifies every component that supports an id attribute and adds a generated, but unique, id for each one of them. Of course, the generation is done in accordance with the indicated id prefix.

See also

The code bundled with this book contains a complete example of this recipe. The project can be opened with Eclipse Ganymede and it can be deployed under JBoss AS with JSF 1.2/2.0 (this is the only Eclipse-based recipe in the book). The project name is: Working_with_JSF_ID_Generator.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset