In this blog I'm going to go over the steps to get a simple HelloWorld web-app up and running on JBoss 6 using JSF 2 and CDI. Background information and full documentation for the technologies used can be found at the Java EE 6 home page and Weld documentation site.

The first step is to set up the project structure. I used Maven 2, Eclipse, and the m2eclipse plugin to create a new Maven Project. I opted not to use an archetype, but there are some good ones from Weld over atSeamframework.org that will essentially produce the same thing. The resulting project structure should look pretty familiar if you've ever used Maven.

Next we need to add the necessary dependencies to our pom.xml:

I decided to preform a simple registration use case with two views, a model, and an action. Before we write any code we need to put an empty beans.xml file in WEB-INF. The presence of this file tells the container that this project contains CDI beans. We also need to configure web.xml to use JSF:

Once those are in place we can create the Registration model:

Note the @Model annotation and some simple Validation-api annotations that will automatically get checked by JSF on form submission. Also note that we don't set the scope of this bean. @Model actually sets the scope to @RequestScoped for us so we don't immediately lose any input values like we would with the default @Dependent scope.

Next we'll create a RegistrationAction:

The @Named annotation makes this a managed bean accessible from the view, and @Inject will create an injection point for our model. For simplicity's sake I'm just returning the name of the view from this method, but a better way would be to use the robust navigation rule framework provided by JSF.

Finally we'll create the two views, register.xhtml and success.xhtml:

I wanted to experiment with the new tag so I'm using it to re-render the name that's typed in on each keyup event.

Now we just use Maven to build and deploy to JBoss (with the optional JBoss plugin) and the site should look like this:

And that's it! When the Submit button is pressed our model will be validated, injected into the RegistrationAction, and the register() method will execute, returning the success view.