Skip navigation
2010

Johnny ren's Blog

December 2010 Previous month Next month

Why Facelets not JSP?


  • templating and composite components

 

The main benefit of using Facelets over JSF is that Facelets supports code reuse through templating and composite components.

 

The example below shows how the address.xhtml template is used in the useraddresses.xhmtl file. A user has a mailing address and a shipping address. The address.xhtml is used twice in the useraddresses.xhtml. The big difference between Facelets template and JSP include is that you can pass object parameters from the including file to the template in Facelets and you can’t do that from JSP including file.

 

 

address.xhtml

 

 

address.bmp

 

 

useraddress.xhtml

 

 

useraddress.bmp

 

 

 

 

 


 

What is the difference between Seam Managed Persistence and EJB managed Persistence Context?

 

smpc.bmp

  • Transactional scoped EJB Persistence Context is bound to EJB transaction. You may get lazyinitializationexception when you try to display JPA lazy initialized properties in the next JSF page.
  • Extended Persistence Context is bound to EJB Stateful session bean. You will not get lazyinitializationexception. 
  • Seam Managed Persistence Context can be used in POJO while EJB managed PC is only available in EJB.  SMPC is default conversion scope.   You don’t have to worry about the Lazyinitializationexception when you try to display JPA lazy initialized properties in the next JSF page.

 

Understanding JBoss Seam Contextual Dependency Injection

 

WHY Contextual Dependency Injection?

  • Dependency injection promotes loose coupling design pattern. Traditionally, the client is responsible for creating the dependent object or looking up a service.  Therefore, it creates a tight coupling between the client and dependent object.
  • Seam allows developers to inject Seam container managed object into Seam component.  Seam container is responsible for creating and managing the life cycle of the object.  Therefore, the client is not dependent on the implementation of the object.
  • Each object managed by Seam has a well defined scope that is bound to a specific context. Seam extends servlet context model.

 

How does Seam Inject work?

dependent injection.bmp

 

 

  • Client calls the register method.
  • Seam intercepts the call and injects user object and Entity manager just before invoking the register method.
  • Seam outject components annotated with “@Out”
  • Seam reset the injected attribute to null to get ready for next call.

 

Seam context scope

scope.bmp

 

 

 

  • Event scoped components last from the beginning of the JSF request to the end of request.
  • Default conversion scoped component last from the beginning of the JSF request to the end of request.   Default conversion scope component survives page redirect.
  • Page scoped component lasts from the beginning of the JSF request until the user leaves the page.   Page scoped components are stored in the JSF component tree.

 

 

Component life cycle management

life cycle.bmp

 

  • @In(create=true) tells Seam container to create a component instance if it does not exists in the context
  • @Startup tells Seam container to create a component at initialization time
  • @AutoCreate tells Seam container to create a component automatically
  • @Factory tells Seam container to initialize the component using method annotated if the component does not exist in the context
  • @Out tells seam to outject a component to the context
  • @Create tells Seam to call this method after instantiating an object

 


 

Understanding JBoss Seam Registration example

 

What is JBoss Seam?

  • It is Framework that integrates JSF, EJB 3/POJO and JPA very well.  JSF is a component technology that allows developers to reuse components. There are many JSF component providers, such as Rich faces and Ice faces.  Java Persistence API (JPA) is java standard for mapping java object to a relational database.
  • Seam is also a context based dependency injection framework.  Seam allows developers to inject Seam container managed object into Seam component.  Seam container is responsible for creating and managing the life cycle of the object.  Dependency injection framework promotes loose coupling design pattern.  The client of a java object is not responsible for the creating of the object. Therefore, the client is not dependent on the implementation of the java object.
  • Seam enhanced JSF. It provides a whole solution to an entire web application project.  For example, it provides many extra features such as scheduler and email integration. 

 

The Registration example

JBoss Seam distribution comes with many sample applications.  The registration example allows users to store his username, real name and password in the database.  Blow is a screen shoot from Seam Document.

 

register example.bmp

 

The Entity bean: user.java

user.bmp

 

  • Entity annotation      indicates that the user class is an entity
  • Name annotation indicates      that the user class is a JBoss seam component named “user”
  • Scope annotation indicates      that the context associated with the class
  • NotNull annotation is part      of the Hibernate validator framework that can be used in persistent layer      and JSF layer

 

The Action class: RegisterAction.java

 

action.bmp

  • Stateless annotation      indicates that the Registration class is a stateless session bean
  • In annotation indicates      that  user object will be injected      by Seam container
  • PersistenceContext annotation      indicates that the EntityManager object em will be injected.
  • The register method simply      uses entity manager to persist the user object into database.

 

 

 

 

 

 

The View: Register.xhtml

view.bmp

  • S:validateAll tag tell      Seam to validate all input fields against Hibernate validator
  • JSF facelet EL      “#user.username” binds the username input field to the username attribute      of the “user” Seam component.
  • The commandButton tag binds      the “register” button to the “register” method of “register” component.  When a user clicks on the register      button, the register method will be invoked.

The View: Registered.xhtml

 

registered.bmp

  • When the register method      is successfully executed, the system will then display the registered message.