ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

JBoss Seam

by Thomas Heute

JavaServer Faces (JSF) and Enterprise JavaBeans (EJB) 3.0 are great technologies for building web applications. There is a significant deal of synergy between the two frameworks: stateless and stateful EJBs are excellent event-handling backing beans for JSF. EJB entity beans provide automatic and transparent object/relational database mapping; hence, they are a fine solution for implementing an object-oriented domain model. Visible JSF components can bind their state to entity beans. You may even want to add some business process management (BPM) into your application.

However, while the above described synergy between EJB 3.0 and JSF is appealing, it is not yet realized within the Java Enterprise Edition (JEE) 5.0 framework. In plain Java EE, there is still a significant amount of glue code necessary to use both JSF and EJB 3.0 when building a web application--even more, if you add BPM to the recipe for a great application. In particular, many problems inherent to the web application, such as the dreaded "Back button problem" or multi-window operations, require time spent on the infrastructure coding rather than on the actual business problem.

The JBoss Seam framework is designed to take care of the plumbing between existing frameworks including EJB 3.0, JSF, and BPM. The Seam stateful component model makes it a breeze to develop sophisticated stateful web applications. In this article, we will show how to use Seam to create an e-commerce application--the JBoss Seam DVD Store application.

Bringing the EJB and Web Together

Traditionally, developers apply many design patterns in a sophisticated web application, creating layers of code, which is often a requirement of the frameworks, not of the application design. Seam collapses these artificial layers and brings EJBs closer to the web layer. It allows you to access EJBs as JSF backing beans. Hence, a developer is free to choose what layers are needed according to the application's architecture requirements, instead of being forced into decisions by the framework.

Seam manages the lifecycles of the application components automatically. The developer interacts with components by retrieving and storing them from and into the Seam context. This design is also known as "Inversion of Control," and it frees the developers from manually managing the relationships between the components.

To illustrate our point, let's look an example from a hypothetical DVD store. This example shows how to retrieve orders already made by the logged-on user on a single web page. Let's first have a look at our stateful session bean, which executes the business logic and performs all database-related operations. This session bean will be in charge of finding all of the orders of a logged-in user. Please note that we chose to show a simple way to perform this task. You can certainly keep your habits of having value objects in between, and remember that Seam components can be any object, not only EJBs.

package com.jboss.dvd.seam;
import [...]

public class ShowOrdersAction
    implements ShowOrders,
    Customer customer;

    EntityManager em;

    List <Order> orders;    

    public String findOrders() {
        orders = em.createQuery(
   "from Order o where o.customer = :customer")
            .setParameter("customer", customer)
        return "showorders";

    public void destroy() {}

This bean is a standard EJB3 stateful bean. The @Stateful annotation is used to declare that bean as a stateful session bean and the @Remove annotation is used to ask the server to remove that bean once the destroy method is called.

The session bean class also has some annotations beyond the EJB 3.0 specification. These are Seam annotations. The @Name annotation gives a reference name to the session bean so that it can be called from any JSF page or other Seam components. We also specify the SeamInterceptor as the only interceptor for this session bean. This is required for Seam to work out the "plumbing" for the bean.

Pages: 1, 2, 3, 4, 5

Next Pagearrow