JBoss.comEnterprise Documentation

Chapter 3. Write Your First Project with JBoss Developer Studio

3.1. Create a Seam Application
3.1.1. Start Development Database
3.1.2. Create and deploy Seam Web Project
3.1.3. Start JBoss Application Server
3.1.4. Workshop Project Code Overview
3.2. Seam Action Development
3.2.1. Create a New Seam Action
3.2.2. Test Seam Action
3.2.3. Modify Seam Action User Interface
3.3. Declarative Security
3.3.1. Edit Login Authentication Logic
3.3.2. Secure Seam Page Component
3.4. Browsing Workshop Database
3.4.1. Database Connectivity Setup
3.4.2. Browse Workshop Database
3.5. Database Programming
3.5.1. Reverse Engineer CRUD from a Running Database
3.5.2. Use Hibernate Tools to Query Data via JPA
3.5.3. Use Hibernate Tools to visualize the Data Model
3.6. Rich Components
3.6.1. Add a Richfaces component to the CRUD Application

This chapter is a set of hands-on labs. You get step-by-step information about how the JBoss Developer Studio can be used during the development process.

In this section you get to know how to create a Seam project in JBDS, how to start the server and what a structure your project has after creating.

Before opening the JBoss Developer studio you need to download and start a Workshop Database.

To start the database just run ./runDBServer.sh or runDBServer.bat from the database directory.

The end result should be a console window that looks like:


Minimize the terminal window and run the JBoss Developer Studio from Applications Menu or from the desktop icon.

First of all you get the Workspace Launcher. Change the default workspace location if it's needed. Click on Ok.


After startup, you see the welcome page. You could read how to work with welcome pages in previous chapter. Now select Create New... icon and then press on Create Seam Project link.

The New Seam Project wizard is started. You need to enter a name (e.g., "workshop") and a location directory for your new project. The wizard has an option for selecting the actual Server (and not just WTP runtime) that will be used for the project. This allow the wizard to correctly identify where the needed datasource and driver libraries need to go. All settings are already specified. Click on Next.


Next, you will be asked to select the "features" you want to use in your project. This allows JBoss Developer Studio to setup the appropriate tooling for your project. Since JBoss Seam integrates all popular Java EE frameworks, you can select any combination of technologies from the list. Here, for this project, we will select Dynamic Web Module, Java, JavaServer Faces (JSF), and Seam Facet for a typical database-driven web application. The default project facets should suffice, click on Next.


In the Project Facets Specifying form you can also bring up server runtimes panel by clicking Show Runtimes in the bottom right corner. This panel shows available server runtimes. Then this button will be changed into Hide Runtimes one.

Click Next to proceed further.


A dynamic web application contains both web pages and Java code. The wizard will ask you where you want to put those files. You can just leave the default values or choose another folder.


On the next form, you will be able to select where those library JARs come from. The easiest is just to select the JARs provided by the JBoss AS runtime associated with this project. That is why it is important to chose the right JBoss AS 4.2 runtime in the project setup window.


Next wizard step needs more settings that previous. Let's start with General section.

Leave the default Seam runtime and check a WAR deployment.


Next Database section is a little tricky. The Connection Profile needs to be edited so that the new project works properly with the external HSQLDB server. By default the project wizard tries to use the JBoss embedded HSQLDB, but the tutorial uses an external database to replicate a more real world development scenario. Click on Edit to modify the Connection Profile.


Select JDBC Connection Properties. Make sure the URL is set to jdbc:hsqldb:hsql://localhost:1701


Try click on Test Connection button. It probably won’t work. This happens if the hsql jdbc driver is not exactly the same. This can be worked around by modifying the HSQLDB database driver settings. To modify the settings, click the “...” next to the drop-down box.

The proper Driver JAR File should be listed under Driver File(s). Select the hsqldb.jar file found in the database/lib directory and click on Ok.


Select Hypersonic DB and click on Ok. Again, this only happens if the selected hsqldb.jar is different from the running database.


Now, the Test Connection should succeed. After testing the connection, click on Ok.


You can leave the Code Generation section as is. It refers to Java packages in which the generated code will be placed.


Click on Finish button. Now, there should be a new Seam project called “workshop” listed in Package Explorer view.


Now let's examine the project and its structure. Go back to the Package Explorer view in JBoss Developer Studio.

It seems like it's not much for project but this shell application contains a login screen with default login logic, a menu template that can be further modified, and other layout templates.


It’s important to note that the business logic will reside in the src/action folder, by default. And, the package naming conventions that were used in Seam project setup wizard could have been changed to something different from org.domain.workshop.session. Also, notice that there is a default Authenticator. java file. This is where custom security logic can be added. Seam has a nice declarative security model that we will explore in a bit more detail later on. The src/model folder will store the project’s JPA entity beans. The view tier of the application is also important. Seam uses facelets and there is a built-in facelets GUI editor that has some nice WYSIWYG and component drag/drop functionality. Try this out by opening home.xhtml from WebContent folder.


Notice that the templates reside in the WebContent/layout folder. There is a stylesheet in the WebContent/stylesheet folder. There is also a login and default error page. The Facelet editor will be explored in more detail later in the lab.

The project already has a datasource that was created per the Seam project wizard database settings. And, obviously all of the Seam specific configuration files and JAR dependencies are included and placed in the proper locations. On last noteworthy line item is related to the build script. There isn’t a build script because the Eclipse WTP(Web Tools Project) plugin is used to publish web application changes. As you can see, JBoss Developer Studio is removing a great deal of complexity from the enterprise Java project setup/deployment process. The end result is a developer that is writing code, not spending days/weeks trying to figure out how to get a decent development environment and project build process.

Now, it’s time to write some code. The good news is that JBoss Developer Studio can also help out in this respect. In this section, we will create a new Seam Action POJO and facelet with some custom business logic and some GUI changes.

In this section you will see how it’s easy to secure the facelets and facelet components in Seam. Let’s go ahead and secure the action button, then we will secure the entire page.

In this section you get to know how to use the workshop database that was started at the beginning of the lab.

Now, it’s time to reverse engineer the workshop database into a fully functioning Seam CRUD(Create Read Update Delete) application.

Now, it’s time to write some JPA queries using the Hibernate Tools perspective in JBoss Developer Studio.

In the upper right corner of the workbench there is a small >>, click on it and choose Hibernate.


Look at the Hibernate Configurations view. In the "workshop" project, drill down on the Session Factory and notice that the JPA entities/attributes are listed in a nice tree view.


Right click on the Session Factory and select HQL Editor. This will open a JPA query scratch pad window.

Write your query and click on the "Hibernate Dynamic SQL Preview" tab. You should see the SQL that will be executed if this JPA query is run.


Run the query by clicking on the green run icon.

The results are listed in the "Hibernate Query Result" tab. There is a "Properties" tab in the workbench that can be used to see a specific JPA result. These results represent the JPA objects because our query did not specify column names.


The query can be refined, and take note that there is nice code completion in the JPA query editor.


A refined query will return results that are more ResultSet oriented. Notice the join logic that JPA supports.


There was no need to specify an Employees table in the from part of the JPA query because JPA supports reference traversal via Java class attribute references. Not only are JPA and HQL queries fully supported, but Criteria based queries can also be written in the Criteria Editor. You should spend some time tinkering with different queries and possibly Criteria based queries, even though the instructions are not provided in this lab.


This lab will conclude with one last AJAX twist. In this section we add a RichFaces inputNumberSlider to the Order Details edit screen.