Seam 2 with Maven - a no nonsense example

Posted by Alistair Orchard
Posted: 2/1/2010

Part 1: setting up the project and having a poke around...

Project setup

Hello and welcome to my first blog post, in this post I am going to be having a quick look at the framework of Seam using Maven, to project manage our internal libraries. I am going to talk you through here how to do this with me.

Why am I choosing Maven? Well because it is excellent for sorting all our library problems for us - it grabs the jars and organises them nicely in a repository.

It is perfect for both small and large projects and excellent to use in a team environment. With Seam 2, when the POM is updated in the source repository, everyone will automatically have their libraries updated to the correct version.

Here is an example; in this example it assumes that you have already installed Maven2 on your machine, if you haven't then it is the first unwritten step. It can be downloaded here http://maven.apache.org/

The first step is going to be importing a pre-prepared POM into intellij idea - the steps to open this in eclipse should be similar.

First of all create a new project and select 'import from external model', next on the next page select Maven and specify the root directory of your project - this will be the root of the directory you'll unzip the source provided into.

As this is a good opportunity, it would be wise to use the advanced options and path intellij to your maven 2 installation directory root. As shown here:

Please now continue with the project setup, using all the defaults provided to you. You should now have a nice intellij project using maven. Be aware that when you start up the project, it will start to automatically download libraries for you. Intellij will find all the dependencies of the project, the library dependencies and start to download these as the source and javadoc where necessary.

Should you want to to play with the way Maven works in inteliij , you will find Maven settings under 'Settings's and 'Template Settings' .

Project configuration

This is a very basic setup with all the frameworks in place to expand and create a working website with full back-end functionality. Seam is an excellent interface framework and coupled with Hibernate, using the relatively new annotation-based configuration, it will blend perfectly together.

There are a few things to take note of in the source provided, navigate yourself to the src/main/resources directory and you'll find hibernate.cfg.xml - this will have to be configured, to point to your database, which will require a user with sufficient privileges to create/drop tables and insert/delete/update to said tables. In here you will also find your log4j properties, the messages, en.properties which is used for i18n and seam.properties.

I will look at all of these files in more detail in another post.

Some further important configuration can be found in the src/main/webapp/WEB-INF directory and sub-directories. You will find the following.

  • /navigation/pages.xml - This is used for the configuration of your pages within the site, allowing you to specify things like rewrite rules to transform the jsf style path to a nice RESTful path. i.e. /pages/error/errorPage.seam?id=cheese -> /error?id=cheese This can also be used to configure basic authentication, specify the login page, error page, access denied page, etc.
  • /tld/mySite.page.taglib.xml - This is a jsf library so that we can create our own tags for duplicate content and templating.
  • /beans.xml - This is a Spring configuration file which will allow us later in this example to configure a Web Service (REST based) interface with which we will mix some Jetty into, which in turn will allow us to talk to Ajax in our website using JSON - the easy way!
  • /components.xml - This is basically where our seam configuration resides. One tag to take note of is 'security:identity' as this is what we use to overload the default identity class in Seam
  • /faces-config.xml - JSF and faces configuration file, this will tell JSF which EL resolvers to use etc. Nothing exciting happening here at the moment.
  • /urlrewrite.xml - A wonderful library! This will allow us to do even more than simply specify a rewrite for our pages into a RESTful style url. In this we could for instance take a high ranking google link from a previous website incarnation and graft it into our new structure. We can also specify what happens to the url, based upon criteria such as the browser agent, for crawlers and robots we can stop URL's from appearing the same way they do for users or redirect them to a different page, anything we want.
  • /web.xml - This is the configuration central of our site, it is currently set up to use all the libraries and configuration that we have set up in the project so dabble here with caution, things might just stop working!

Building the project

The last thing we have to do for this post is actually build the application, there are several ways to do this but by far the easiest is to use IntelliJ to build us the WAR file.

On the right hand site you'll find a Maven pane within which, you can find the install command, if you run this it will build the site and place the jar into your Maven repository, which by default will sit within your user directory. Ex. /Documents & Settings/Users/Alistair/.m2/ and if you have left the namespace the same it will path out to com/website/

That should just about do it for my first post!

Now you may experience some teething problems, with setting the project up, but play with it for a while, everything you need is provided in the example project. Once the project is set up correctly, you'll be able to build it into a WAR, which can be deployed on an application server like Tomcat. Please remember, that you'll need to set up a database and change the hibernate configuration, if you want it to do anything! And when you manage to get it working, the default username/password is alistair/password (when you are prompted in the user/roles page)

Next time we will get our hands dirty with a bit of richfaces, seam configuration and Hibernate, when we look at authentication and managing users with permissions.