SlideShare a Scribd company logo
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

More Related Content

PDF
Bt0083 server side programing 2
PPTX
Jsp presentation
PPT
Java Server Pages
PPT
Java Server Faces (JSF) - advanced
PPT
KMUTNB - Internet Programming 5/7
PPT
Web Applications and Deployment
ODP
Java Persistence API
PPTX
Servlet and jsp interview questions
Bt0083 server side programing 2
Jsp presentation
Java Server Pages
Java Server Faces (JSF) - advanced
KMUTNB - Internet Programming 5/7
Web Applications and Deployment
Java Persistence API
Servlet and jsp interview questions

What's hot (19)

PDF
Introduction to Polymer and Firebase - Simon Gauvin
PPTX
Implicit objects advance Java
PPT
PDF
Java Web Programming [8/9] : JSF and AJAX
PPTX
Implicit object.pptx
PPTX
Jsp Introduction Tutorial
PPT
Data Access with JDBC
PDF
Trustparency web doc spring 2.5 & hibernate
PDF
Design patterns in Magento
DOCX
Java beans
PDF
Design patterns in PHP
PPS
JSP Error handling
PPT
JavaScript
PDF
Java Web Programming [5/9] : EL, JSTL and Custom Tags
PDF
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
PPT
Jsp/Servlet
PPTX
PPT
hibernate with JPA
Introduction to Polymer and Firebase - Simon Gauvin
Implicit objects advance Java
Java Web Programming [8/9] : JSF and AJAX
Implicit object.pptx
Jsp Introduction Tutorial
Data Access with JDBC
Trustparency web doc spring 2.5 & hibernate
Design patterns in Magento
Java beans
Design patterns in PHP
JSP Error handling
JavaScript
Java Web Programming [5/9] : EL, JSTL and Custom Tags
#31.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_스프링프레임워크 강좌, 재직자환급교육,실업자국비지원...
Jsp/Servlet
hibernate with JPA
Ad

Viewers also liked (8)

PDF
Os Pittaro
PDF
Os Schlossnagle Theo
PDF
Os Fetterupdated
PPT
Os Nolen Gebhart
PPT
Os Lonergan
PDF
Os Nightingale
PPT
Os Vandeven
PDF
J Ruby Whirlwind Tour
Os Pittaro
Os Schlossnagle Theo
Os Fetterupdated
Os Nolen Gebhart
Os Lonergan
Os Nightingale
Os Vandeven
J Ruby Whirlwind Tour
Ad

Similar to Os Leonard (20)

ODP
JavaEE Spring Seam
PPT
Seam Introduction
PPT
PPT
Struts2
PPTX
Custom Action Framework
PPT
Spring Capitulo 05
PPT
Struts Overview
PPTX
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
PPT
Struts 2 + Spring
ODP
jBPM5 in action - a quickstart for developers
ODP
ActiveWeb: Chicago Java User Group Presentation
PPT
Introducing Struts 2
PPT
PPT
Struts,Jsp,Servlet
PPT
Strutsjspservlet
PPT
Strutsjspservlet
ODP
A Complete Tour of JSF 2
PPT
Jsfsunum
ODP
java ee 6 Petcatalog
PPT
Facelets
JavaEE Spring Seam
Seam Introduction
Struts2
Custom Action Framework
Spring Capitulo 05
Struts Overview
[DSBW Spring 2009] Unit 07: WebApp Design Patterns & Frameworks (3/3)
Struts 2 + Spring
jBPM5 in action - a quickstart for developers
ActiveWeb: Chicago Java User Group Presentation
Introducing Struts 2
Struts,Jsp,Servlet
Strutsjspservlet
Strutsjspservlet
A Complete Tour of JSF 2
Jsfsunum
java ee 6 Petcatalog
Facelets

More from oscon2007 (20)

ODP
Solr Presentation5
PDF
Os Borger
PDF
Os Harkins
PDF
Os Fitzpatrick Sussman Wiifm
PDF
Os Bunce
PDF
Yuicss R7
PDF
Performance Whack A Mole
ODP
Os Fogel
PDF
Os Lanphier Brashears
PPT
Os Tucker
PDF
Os Fitzpatrick Sussman Swp
PDF
Os Furlong
PDF
Os Berlin Dispelling Myths
PDF
Os Kimsal
PDF
Os Pruett
PDF
Os Alrubaie
PDF
Os Keysholistic
ODP
Os Jonphillips
PDF
Os Urnerupdated
ODP
Adventures In Copyright Reform
Solr Presentation5
Os Borger
Os Harkins
Os Fitzpatrick Sussman Wiifm
Os Bunce
Yuicss R7
Performance Whack A Mole
Os Fogel
Os Lanphier Brashears
Os Tucker
Os Fitzpatrick Sussman Swp
Os Furlong
Os Berlin Dispelling Myths
Os Kimsal
Os Pruett
Os Alrubaie
Os Keysholistic
Os Jonphillips
Os Urnerupdated
Adventures In Copyright Reform

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Encapsulation theory and applications.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Electronic commerce courselecture one. Pdf
PPTX
A Presentation on Artificial Intelligence
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Understanding_Digital_Forensics_Presentation.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
Encapsulation theory and applications.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Unlocking AI with Model Context Protocol (MCP)
Network Security Unit 5.pdf for BCA BBA.
Reach Out and Touch Someone: Haptics and Empathic Computing
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Spectral efficient network and resource selection model in 5G networks
Dropbox Q2 2025 Financial Results & Investor Presentation
Electronic commerce courselecture one. Pdf
A Presentation on Artificial Intelligence
“AI and Expert System Decision Support & Business Intelligence Systems”
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
Review of recent advances in non-invasive hemoglobin estimation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Digital-Transformation-Roadmap-for-Companies.pptx

Os Leonard

  • 1. Refactoring to Seam Brian Leonard Technology Evangelist Sun Microsystems, Inc.
  • 2. AGENDA The Java EE 5 Programming Model Introduction to Seam Refactor to use the Seam Framework Seam Portability Q&A
  • 3. Java EE 5 Programming Model Registration Application Managed Bean Entity Class
  • 5. Java EE 5 Programming Model JSF Context DB Registration Application Managed Bean JSF Components Action Class Entity Class RegisterActionBean User ManagedBean
  • 6. 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
  • 7. 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”
  • 8. 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;
  • 9. 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); }
  • 10. 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
  • 11. 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
  • 12. 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
  • 13. 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...
  • 14. 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);
  • 15. 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; }}}
  • 16. AGENDA The Java EE 5 Programming Model Introduction to Seam Refactor to use the Seam Framework Seam Portability Q&A
  • 17. 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
  • 18. 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
  • 19. AGENDA The Java EE 5 Programming Model Introduction to Seam Refactor to use the Seam Framework Seam Portability Q&A
  • 20. Seam Registration Application JSF Context DB Action Class Entity Class Seam Framework JSF Components RegisterActionBean User
  • 21. 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
  • 22. DEMO
  • 23. 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.
  • 24. DEMO
  • 25. 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; }...
  • 26. 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; }
  • 27. 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; }}}
  • 28. AGENDA The Java EE 5 Programming Model Introduction to Seam Refactor to use the Seam Framework Seam Portability Q&A
  • 29. 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
  • 30. DEMO
  • 31. 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.
  • 32. Repeat these demos yourself by visiting my blog at http://weblogs.java.net/blog/bleonard