Handling the Login/Authentication Logic
The project wizard does not only create the login, it also connects the login to a business object in your root package called simply Authenticator.java
. Code all of your login validation in Authenticator.java
and depending on whether it is successful or not, return true or false:
public class Authenticator
@Logger Log log;
@In Identity identity;
public boolean authenticate()
log.info("authenticating #0", identity.getUsername());
//write your authentication logic here,
//return true if the authentication was
//successful, false otherwise
Creating JPA Entities
Right-click on your project node in Eclipse's Project Explorer and invoke the New -> Seam Generate Entities menu option. This will trigger a new wizard that will automatically generate JPA entities for you, using the connection profile defined during project creation (see Figure 13). Not only that, but it will also create the basic maintenance web pages for each of the entities, saving you countless hours of repetitive work.
|Figure 13. Generate Seam Entities Wizard: A new wizard automatically generates JPA entities for you, using the connection profile defined during project creation.|
The wizard worked fine for the trivial data model I made for this example, but when I ran it across a production database with hundreds of tables, it consistently choked and escaped with an exception. It seemed to try to create entities for every single table in the database and did not allow me to specify which ones to include. However, if I had already created entity classes, it allowed me to select them before generating the related maintenance pages.
Fortunately, for these more complicated database schemas, I was able to use the Dali JPA tools that come with Eclipse IDE for Java EE to create the entities and then import them into my Seam project. However, I lost some of the extra code it generates (such as the default actions and maintenance web pages).
Also, the generated Seam JPA code had to be massaged by hand; I had to remove the questionable inclusion of the actual schema name in the JPA entity. For example, I preferred to replace this:
@Table(name = "author", catalog = "devx")
public classAuthor implements java.io.Serializable
With just a pure table-based annotation:
@Table(name = "author")
public class Author implements java.io.Serializable
I do not quite understand the logic behind including the database name in the actual annotation, as that is really a property of the database connection that the JPA entity manager is using. As such, I believe it does not belong in the actual code.
However, all of these concerns were more than satisfied when Seam generated a perfect set of search/edit pages for each of my JPA entities, complete with navigation and integrated validation. All that was left was to do some minor massaging of the HTML to correct the layout and replace the default labels with something more user-friendly.
Last but not least, the default Facelets template (located in WebContent/layout/template.xhtml) can be modified to change how the default links to each of the generated pages are displayed (see Figure 14).
|Figure 14. A Generated Search/Edit Page for a JPA Entity: The default Facelets template (located in WebContent/layout/template.xhtml) can be modified.|
Creating a New Conversation
Instead of simply auto-generating pages for your JPA entities, it is also possible to create your own Seam conversations (i.e., a web page flow spanning multiple pages but represented by a single backing JSF bean). This involves invoking the New Seam Conversation wizard and specifying the default settings (see Figure 15).
The wizard generates the basic JSF backing bean as well as the initial start page, ready for your customizations (see Figure 16). Please note, however, that it always places that page in the root web folder, so for large applications you may want to move it manually to a module-specific subfolder.
Figure 15. New Seam Conversation Wizard: Create your own Seam conversations by invoking the New Seam Conversation wizard and specifying the default settings.
Figure 16. Default Start Page Generated by the New Seam Conversation Wizard: The wizard generates the basic JSF backing bean as well as the initial start page.
Smooth Sailing After Rough Start
Although my initial JBoss Tools experience was anything but smooth (See Sidebar 2), after correcting my runtime settings everything started to work much better. In particular, the support for generating entities from JPA entities, integrated Ajax support in RichFaces, and Seam conversation functionality convinced me how productive one can be with this integrated combination of tools and frameworks. After spending months developing old school Struts actions and coding Ajax by hand on a recent project, it was truly a breath of fresh air.