JBoss Seam: Contextual Components - PowerPoint PPT Presentation

About This Presentation
Title:

JBoss Seam: Contextual Components

Description:

Integrates the server layer with the presentation layer. Declarative ... Uses EJB Session Beans as JSF Action Listeners (no 'glue code') No JNDI context lookup ... – PowerPoint PPT presentation

Number of Views:64
Avg rating:3.0/5.0
Slides: 19
Provided by: Jas102
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: JBoss Seam: Contextual Components


1
JBoss Seam Contextual Components
  • Jason Bechtel
  • bechtel2_at_cse.buffalo.edu

2
Overview
  • What is Seam?
  • How does it work?
  • Example Code and what it does
  • Demo
  • How it works
  • Questions

3
What is JBoss Seam?
  • An application framework for Java EE 5
  • A unifier for EJB 3.0 and JSF
  • Integrates the server layer with the presentation
    layer
  • Declarative State Management

4
What is JBoss Seam?
  • Seam works in any application server that
    supports EJB 3.0
  • Use Seam as a framework for applications using
  • JSF (Presentation)
  • Hibernate (or plain JDBC) (Persistence)
  • JavaBeans (Business Logic)
  • Etc.

5
How does it work?
  • 2 JSP pages
  • An Entity Bean
  • A Stateless Session Bean
  • EJB Entity management
  • Uses EJB Session Beans as JSF Action Listeners
    (no glue code)
  • No JNDI context lookup
  • Seam does it all!

6
User.java (Entity Bean)
  • _at_Entity

    (1)
  • _at_Name("user")

    (2)
  • _at_Scope(SESSION)

    (3)
  • _at_Table(name"users")

    (4)
  • public class User implements Serializable
  • private static final long serialVersionUID
    1881413500711441951L
  • private String username
    (5)
  • private String password
  • private String name
  • public User(String name, String password, String
    username)
  • this.name name
  • this.password password
  • this.username username

7
  • (1)The EJB3 standard _at_Entity annotation indicates
    that the User class is an entity bean.
  • (2)A Seam component needs a component name
    specified by the _at_Name annotation. This name must
    be unique within the Seam application. When JSF
    asks Seam to resolve a context variable with a
    name that is the same as a Seam component name,
    and the context variable is currently undefined
    (null), Seam will instantiate that component, and
    bind the new instance to the context variable. In
    this case, Seam will instantiate a User the first
    time JSF encounters a variable named user.
  • (3)Whenever Seam instantiates a component, it
    binds the new instance to a context variable in
    the component's default context. The default
    context is specified using the _at_Scope annotation.
    The User bean is a session scoped component.
  • (4)The EJB standard _at_Table annotation indicates
    that the User class is mapped to the users table.
  • (5)name, password and username are the persistent
    attributes of the entity bean. All of our
    persistent attributes define accessor methods.
    These are needed when this component is used by
    JSF in the render response and update model
    values phases.

8
User.java Cont
  • public User()

    (6)
  • _at_NotNull
  • _at_Length(min5, max15) (7)
  • public String getPassword() return password
  • public void setPassword(String password)
    this.password password
  • _at_NotNull
  • public String getName() return name
  • public void setName(String name) this.name
    name
  • _at_Id
  • _at_NotNull
  • _at_Length(min5, max15)
    (8)
  • public String getUsername() return username
  • public void setUsername(String username)
    this.username username

9
  • (6)An empty constructor is both required by both
    the EJB specification and by Seam.
  • (7)The _at_NotNull and _at_Length annotations are part
    of the Hibernate Validator framework. Seam
    integrates Hibernate Validator and lets you use
    it for data validation (even if you are not using
    Hibernate for persistence).
  • (8)The EJB standard _at_Id annotation indicates the
    primary key attribute of the entity bean.

10
RegistrationAction.java (Session Bean)
  • _at_Stateless (1)
  • _at_Name("register")
  • _at_Interceptors(SeamInterceptor.class) (2)
  • public class RegisterAction implements Register
  • _at_In (3)
  • _at_Valid (4)
  • private User user
  • _at_PersistenceContext (5)
  • private EntityManager em
  • _at_In private
  • FacesContext facesContext (6)
  • _at_IfInvalid(outcomeOutcome.REDISPLAY)
    (7)
  • public String register() (8)
  • List existing em.createQuery("select username
    ..

11
  • (1)The EJB standard _at_Stateless annotation.
  • (2)The SeamInterceptor EJB interceptor must be
    enabled for all session beans which are Seam
    components.
  • (3)The _at_In annotation marks an attribute of the
    bean as injected by Seam. In this case, the
    attribute is injected from a context variable
    named user (the instance variable name).
  • (4)The _at_Valid annotation is provided by Hibernate
    Validator.
  • (5)The EJB standard _at_PersistenceContext
    annotation.
  • (6)Seam also allows various JSF, Seam and jBPM
    context objects to be injected. To get the
    current FacesContext instance injected to a Seam
    component, just mark an instance variable named
    facesContext with the _at_In annotation.
  • (7)The _at_IfInvalid annotation tells Seam to
    validate the component state using Hibernate
    Validator before invoking the action listener
    method, and return a different JSF outcome if the
    state is invalid. In this example, the user is
    validated when the register() method is invoked,
    and the form is redisplayed with messages if a
    validation failure occurs.
  • (8)The action listener method uses the standard
    EJB3 EntityManager API to interact with the
    database, and returns the JSF outcome. Note that,
    since this is a sesson bean, a transaction is
    automatically begun when the register() method is
    called, and committed when it completes.

12
faces-config.xml
  • lt?xml version"1.0" encoding"UTF-8"?gt
  • lt!DOCTYPE faces-config
  • PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer
    Faces Config 1.0//EN" "http//java.sun.com/dtd/web
    -facesconfig_1_0.dtd"gt
  • ltfaces-configgt
  • ltnavigation-rulegt
  • ltnavigation-casegt
  • ltfrom-outcomegtsuccesslt/from-outcomegt
  • ltto-view-idgt/registered.jsplt/to-view-idgt
    lt/navigation-casegt
  • lt/navigation-rulegt
  • lt!-- A phase listener is needed by all Seam
    applications --gt
  • ltlifecyclegt
  • ltphase-listenergtorg.jboss.seam.jsf.SeamPhaseList
    ener
  • lt/phase-listenergt
  • lt/lifecyclegt
  • lt/faces-configgt

13
View pages using JSP with JSF
  • lt_at_ taglib uri"http//java.sun.com/jsf/html"
    prefix"h" gt
  • lt_at_ taglib uri"http//java.sun.com/jsf/core"
    prefix"f" gt
  • lthtmlgt
  • ltheadgt
  • lttitlegt
  • Register New User
  • lt/titlegt
  • lt/headgt
  • ltbodygt
  • ltfviewgt
  • lthformgt
  • lttable border"0"gt
  • lttrgt
  • lttdgtUsernamelt/tdgt
  • lttdgtlthinputText value"user.usernam
    e"/gtlt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtReal Namelt/tdgt
  • lttdgtlthinputText value"user.name"/
    gtlt/tdgt
  • lt_at_ taglib uri"http//java.sun.com/jsf/html"
    prefix"h" gt
  • lt_at_ taglib uri"http//java.sun.com/jsf/core"
    prefix"f" gt
  • lthtmlgt
  • ltheadgt
  • lttitlegt
  • Successfully Registered New User
  • lt/titlegt
  • lt/headgt
  • ltbodygt
  • ltfviewgt
  • Welcome,
  • lthoutputText value"user.name"/gt
  • , you are successfully registered as
  • lthoutputText value"user.username"/gt
  • lt/fviewgt
  • lt/bodygt
  • lt/htmlgt

14
application.xml
  • ltapplicationgt
  • ltdisplay-namegtSeamlt/display-namegt
  • ltmodulegt
  • ltwebgt
  • ltweb-urigtjboss-seam-registration.warlt/web-ur
    igt
  • ltcontext-rootgt/seam-registrationlt/context-ro
    otgt
  • lt/webgt
  • lt/modulegt
  • ltmodulegt
  • ltejbgtjboss-seam-registration.jarlt/ejbgt
  • lt/modulegt
  • lt/applicationgt
  • This deployment descriptor links modules in the
    enterprise archive and binds the web application
    to the context root /seam-registration.
  • We've now seen most of the files in the
    application!

15
Demo
16
How it works
  • When the form is submitted, JSF asks Seam to
    resolve the variable named user. Since there is
    no value already bound to that name (in any Seam
    context), Seam instantiates the user component,
    and returns the resulting User entity bean
    instance to JSF after storing it in the Seam
    session context. JSF binds the form input values
    to properties of the User entity bean.
  • Next, JSF asks Seam to resolve the variable named
    register. Seam finds the RegisterAction stateless
    session bean in the stateless context and returns
    it. JSF invokes the register() action listener
    method.
  • Seam intercepts the method call, injects the User
    entity from the session context, and the current
    FacesContext instance, before asking Hibernate
    Validator to validate the session bean instance
    (and, recursively, the User entity bean
    instance). If the state is valid, the invocation
    proceeds and the register() method is called. If
    not, Seam returns a null outcome and JSF
    redisplays the page.
  • When JSF comes to render the next JSP page, it
    asks Seam to resolve the variable named user and
    uses property values of the returned User entity
    from Seam's session scope.

17
References
  • http//labs.jboss.com/portal/index.html?ctrlidpa
    ge.default.infoprojectjbossseam
  • http//labs.jboss.com/portal/jbossseam/gettingstar
    ted
  • http//docs.jboss.com/seam/reference/en/html/index
    .html

18
Questions?
Write a Comment
User Comments (0)
About PowerShow.com