• Save
Os Leonard
Upcoming SlideShare
Loading in...5
×
 

Os Leonard

on

  • 1,259 views

 

Statistics

Views

Total Views
1,259
Views on SlideShare
1,259
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Os Leonard Os Leonard Presentation Transcript

  • Refactoring to Seam
    • Brian Leonard
    • Technology Evangelist
    • Sun Microsystems, Inc.
  • AGENDA
      • The Java EE 5 Programming Model
      • Introduction to Seam
      • Refactor to use the Seam Framework
      • Seam Portability
      • Q&A
  • Java EE 5 Programming Model Registration Application Managed Bean Entity Class
  • DEMO
  • Java EE 5 Programming Model JSF Context DB Registration Application Managed Bean JSF Components Action Class Entity Class RegisterActionBean User ManagedBean
  • register.jsp: JSF In Action <td> Username </td> <td> < h:inputText id = &quot;userName&quot; value = &quot; #{ user.username } &quot; required = &quot;true&quot; > < f:validateLength minimum = &quot;5&quot; maximum = &quot;15 &quot; / > <td> Real Name </td> <td> < h:inputText id = &quot;name&quot; value = &quot; #{ user.name } &quot; required = &quot;true&quot; > <td> Password </td> <td> < h:inputSecret id = &quot;password&quot; value = &quot; #{ user.password } &quot; required = &quot;true&quot; > < f:validateLength minimum = &quot;5&quot; maximum = &quot;15 &quot; / > < h:commandButton id = &quot;registerCommand&quot; type = &quot;submit&quot; value = &quot;Register&quot; action = &quot; #{ user.register } &quot; / > A JSF Validator
  • register.jsp & BackingBean
    • ...
    • <td> Username </td>
    • <td> < h:inputText
    • id = &quot;userName&quot;
    • value = &quot; #{ user.username} &quot;
    • ...
    • <td> Real Name </td>
    • <td> < h:inputText
    • id = &quot;name&quot;
    • value = &quot; #{ user.name} &quot;
    • ...
    • <td> Password </td>
    • <td> < h:inputSecret
    • id = &quot;password&quot;
    • value = &quot; #{ user.password} &quot;
    • ...
    • < h:commandButton
    • id = &quot;registerCommand&quot;
    • type = &quot;submit&quot; value = &quot;Register&quot;
    • action = &quot; #{ user.register} &quot; / >
    • ...
    ManagedBean username name password register “ user”
  • Managed Beans Configuration
    • ...
    • <managed-bean>
    • < managed-bean-name > user </managed-bean-name>
    • < managed-bean-class >
    • org.examples.jsf. ManagedBean
    • </managed-bean-class>
    • <managed-bean-scope> request </managed-bean-scope>
    • </managed-bean>
    • ...
    faces-config.xml
    • <td> < h:inputText
    • id = &quot;userName&quot;
    • value = &quot; #{ user.username} &quot;
  • Managed Bean public class ManagedBean { private String username ; private String name ; private String password ; public String getUsername() { return username; } Public String setUsernaame(String username) { this.username=username; } ... private RegisterActionLocal registerActionBean; private InitialContext ctx; { try { ctx = new InitialContext(); registerActionBean = (RegisterActionLocal) ctx.lookup(&quot;registration/RegisterActionBean/local&quot;); } catch (NamingException ex) { ex.printStackTrace(); } } public String register () { return registerActionBean.register(username, name, password); }
  • EJB 3.0
      • Dramatic simplification of all bean types
      • Regular Java classes (POJO)
        • @ Stateless, @ Stateful, @ MessageDriven
        • Use standard interface inheritance
      • Dependency injection
        • Instead of JNDI
      • Interceptors
      • Entity Beans (CMP) replaced with JPA
  • Java Persistence API (JPA)
      • Single persistence API for Java EE AND Java SE
      • Much simpler the EJB CMP
      • At least three implementations (all open source):
        • Hibernate (JBoss)
        • TopLink Essentials (Oracle)
        • Kodo/Open JPA (BEA)
      • Configured via persistence.xml
  • JPA – Object Relational Mapping
      • Developer works with objects
        • Database queries return objects
        • Object changes persist to the database
      • Data transformation is handled by the persistence provider (Hibernate, Toplink, etc.)
      • Annotations define how to map objects to tables
        • @ Entity marks a regular java class as an entity
        • Class atributes map to table columns
          • Can be customized with @ Column
        • Manage relationships: @ OneToMany
  • Our Entity Bean
    • @Entity
    • @Table(name=&quot;users&quot;)
    • public class User implements Serializable{
    • @Id private String username;
    • private String password;
    • private String name;
    • public User(String name, String password,
    • String username){
    • this.name = name;
    • this.password = password;
    • this.username = username;
    • }
    • //getters and setters...
  • JPA Entity Manager
      • Entity Manger stores/retrieves data
      • Inject Entity Manager:
        • @PersistenceContext private EntityManager em;
      • Create an instance of the entity:
        • User u = new User(params);
      • Use Entity Manager methods to persist data:
        • em.persist(u); em.merge(u); em.delete(u);
      • Query using EJB QL or SQL
        • User u = em.find(User.class, param);
  • Our Action Bean
    • @Stateless
    • public class RegisterAction implements Register{
    • @PersistenceContext
    • private EntityManager em;
    • public String register(String username, String name, String password){
    • List existing = em.createQuery (&quot;select username
    • from User where username=:username&quot;)
    • .setParameter(&quot;username&quot;, username )
    • .getResultList();
    • if (existing.size()==0){ // Create a new user
    • User user = new User(username, name, password)
    • em.persist(user) ;
    • return &quot;success&quot; ;
    • } else {
    • FacesContext facesContext = FacesContext.getCurrentInstance();
    • FacesMessage message = new FacesMessage(username + &quot; already exists&quot;);
    • facesContext.addMessage(null, message);
    • return null;
    • }}}
  • AGENDA
      • The Java EE 5 Programming Model
      • Introduction to Seam
      • Refactor to use the Seam Framework
      • Seam Portability
      • Q&A
  • JBoss Seam
      • Application Framework for integrating JSF and EJB 3 component models:
        • Bridge web-tier and EJB tier session contexts
        • Enable EJB 3.0 components to be used as JSF managed beans
      • Integrated AjAX solutions from ICEfaces and Ajax4JSF
      • Improved state management
      • Prototype for JSR 299: Web Beans
  • JBoss Seam Key Concepts
      • Eliminate the ManagedBean – bind directly to our entity and action classes
      • Enhanced context model
        • Conversation
        • Business process
      • Depend less on xml (faces-config)
        • Use annotations instead
      • Bijection – for stateful components
        • Dynamic, contextual, bidirectional
      • Constraints specified on the model, not in the view
  • AGENDA
      • The Java EE 5 Programming Model
      • Introduction to Seam
      • Refactor to use the Seam Framework
      • Seam Portability
      • Q&A
  • Seam Registration Application JSF Context DB Action Class Entity Class Seam Framework JSF Components RegisterActionBean User
  • Integrating the Seam Framework
    • Additions...
      • EJB Module (jar)
        • Include jboss-seam.jar
        • seam.properties
      • Web Module (war)
        • faces-config.xml
            • SeamPhaseListener
        • web.xml
            • jndiPattern
            • SeamListener
  • DEMO
  • faces-config.xml <managed-bean-name> user </... <managed-bean-class>org.ex... 4. ManagedBean userName name password register @Name(&quot; register &quot;) RegisterActionBean user em register 2. User @Name(&quot; use r &quot;) @Scope(ScopeType.EVENT) username name password getters & setters 1. <h:inputText id=&quot;userName&quot; value=&quot;#{ user . username }&quot; ... <h:commandButton ... action=&quot;#{ register . register }&quot;/> ... GONE!!! register.jsp 3.
  • DEMO
  • User.java (1 of 2)
    • @Entity
    • @Name(&quot;user&quot;)
    • @Scope(ScopeType.Event)
    • @Table(name=&quot;users&quot;)
    • public class User implements Serializable{
    • private String username;
    • private String password;
    • private String name;
    • public User(String name, String password,
    • String username){
    • this.name = name;
    • this.password = password;
    • this.username = username;
    • }...
  • User.java (2 of 2)
    • public User() {}
    • @Length(min=5, max=15)
    • public String getPassword(){
    • return password;
    • }
    • public String getName(){
    • return name;
    • }
    • @Length(min=5, max=15)
    • public String getUsername(){
    • return username;
    • }
  • RegisterAction.java
    • @Stateless
    • @Name(&quot;register&quot;)
    • public class RegisterAction implements Register{
    • @In
    • private User user;
    • @PersistenceContext
    • private EntityManager em;
    • public String register(){
    • List existing = em.createQuery(&quot;select username
    • from User where username=:username&quot;)
    • .setParameter(&quot;username&quot;, user.getUsername() )
    • .getResultList();
    • if (existing.size()==0){
    • em.persist(user);
    • return &quot;success&quot; ;
    • }else{
    • FacesMessages.instance().add(&quot;User
    • #{user.username} already exists&quot;);
    • return null;
    • }}}
  • AGENDA
      • The Java EE 5 Programming Model
      • Introduction to Seam
      • Refactor to use the Seam Framework
      • Seam Portability
      • Q&A
  • Seam on GlassFish
      • Add missing JBoss Libraries:
        • hibernate-all.jar
        • thirdparty-all.jar
      • Change Persistence Unit to TopLink
      • Update web.xml
        • JndiPattern: java:comp/env/
        • RegisterAction EJB reference
  • DEMO
  • Summary
    • Hopefully you've learned how to start using the Seam framework in your existing JSF / EJB 3.0 applications.
    • There's much more to Seam, I've just touched the surface.
    • Repeat these demos yourself by visiting my blog at http://weblogs.java.net/blog/bleonard
  • Questions & Answers