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


AddThis Social Bookmark Button

JBoss Seam
Pages: 1, 2, 3, 4, 5

Since the JSF page that uses this session bean needs to retrieve the current orders for a logged-in user, we need to first figure out who the currently logged-in user is. The current user has been stored in the session context just after he successfully logged on, from the session bean, so we just want to inject that entity bean into the "customer" object. @In(value="currentUser", required=false) will then look for a reference to "currentUser" in all of the stateful contexts and will not attempt to create one if it can't find one. Once found, it will be injected in the "customer" object. This operation will happen before any session bean method is called.

After getting the currently logged-in user, we still need to populate the "orders" list so that the JSF page can display. The @Factory("orders") annotation will do the job of populating that object and the annotated method will be called before the JSF page can access the orders.

Now, we have the business component (session bean) to retrieve the orders. Let's look at the JSF web page and how Seam ties the session bean to the JSF page. Basically, in the showorders.jsf page, we just need to reference the session bean and its properties using their Seam names.

<h:dataTable value="#{orders}" var="item">
      <f:facet name="header">Order Id</f:facet>
      <f:facet name="header">Date</f:facet>
      <h:outputText value="#{item.orderDate}">
          <f:convertDateTime type="both" 
      <f:facet name="header">Status</f:facet>

A Contextual Programming Model

Besides EJB 3.0 and JSF integration, Seam is an advanced contextual management framework of its own. It provides crucial features that make it easy to develop stateful web applications. A context is something that is available in many Java programming models. A web application typically has to deal with the Request context, which has the scope of a particular event. The currently logged-in user of the application is held in the Session context. Seam recognizes the importance of these contexts, and adds a few of its own to complete the picture:

  • Conversation: Several related and sequential requests are often considered to be part of the same conversation. For example, a conversation might require that a user goes through the request/response cycle several times. A typical scenario is a wizard-style dialog box, or a shopping cart in an e-commerce application. Seam allows easy definition and management of conversations. While you only have one session at a time, you can have several conversations running concurrently and easily switch from one to the other.

  • Process: Integrating business process management in an application, as provided by tools such as jBPM, is also built-in. Consider the following situation. A business process requires a workflow with two actors: one actor creates an item, while the second actor's responsibility is to approve this item. Both actors might need one or several conversations with the application to complete their task. Seam allows you to define these tasks and conversations very easily and transparently.

  • Application: There is always the need for stateless services and other application-wide components. Seam introduces a particular context for that purpose, available from start to end of the application.

Seam creates all of these contexts and manages their scope automatically. A Seam developer can declare a default context for each component; for example, a User component could have Session scope, representing the currently logged-in user. The power of Seam lies in the wiring of components. A component simply has to declare what dependencies have to be injected into it, before it is executed. A component can also declare what objects should be "taken out" of it, once execution completes. This "outjection" feature, together with the automatic management of contexts and component state, is at the core of programming with Seam and allows very compact and re-usable code.

In the following example, we will look at how we can build a search page to search for DVDs, as illustrated in Figure 1.

Figure 1
Figure 1. The DVD search result page

Pages: 1, 2, 3, 4, 5

Next Pagearrow