Refactoring to Seam <ul><li>Brian Leonard </li></ul><ul><li>Technology Evangelist </li></ul><ul><li>Sun Microsystems, Inc....
AGENDA <ul><ul><li>The Java EE 5 Programming Model </li></ul></ul><ul><ul><li>Introduction to Seam </li></ul></ul><ul><ul>...
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 ...
register.jsp: JSF In Action <td> Username </td> <td> < h:inputText   id = &quot;userName&quot;   value = &quot; #{ user.us...
register.jsp & BackingBean <ul><li>... </li></ul><ul><li><td> Username </td> </li></ul><ul><li><td> < h:inputText   </li><...
Managed Beans Configuration  <ul><li>... </li></ul><ul><li><managed-bean> </li></ul><ul><li>< managed-bean-name > user </m...
Managed Bean public class ManagedBean { private String  username ; private String  name ; private String  password ; publi...
EJB 3.0 <ul><ul><li>Dramatic simplification of all bean types </li></ul></ul><ul><ul><li>Regular Java classes (POJO) </li>...
Java Persistence API (JPA) <ul><ul><li>Single persistence API for Java EE  AND  Java SE </li></ul></ul><ul><ul><li>Much  s...
JPA – Object Relational Mapping <ul><ul><li>Developer works with objects </li></ul></ul><ul><ul><ul><li>Database queries r...
Our Entity Bean <ul><li>@Entity </li></ul><ul><li>@Table(name=&quot;users&quot;)     </li></ul><ul><li>public class User i...
JPA Entity Manager <ul><ul><li>Entity Manger stores/retrieves data </li></ul></ul><ul><ul><li>Inject Entity Manager: </li>...
Our Action Bean <ul><li>@Stateless   </li></ul><ul><li>public class RegisterAction implements Register{  </li></ul><ul><li...
AGENDA <ul><ul><li>The Java EE 5 Programming Model </li></ul></ul><ul><ul><li>Introduction to Seam </li></ul></ul><ul><ul>...
JBoss Seam <ul><ul><li>Application Framework for integrating JSF and EJB 3 component models: </li></ul></ul><ul><ul><ul><l...
JBoss Seam Key Concepts <ul><ul><li>Eliminate the ManagedBean – bind directly to our entity and action classes </li></ul><...
AGENDA <ul><ul><li>The Java EE 5 Programming Model </li></ul></ul><ul><ul><li>Introduction to Seam </li></ul></ul><ul><ul>...
Seam Registration Application  JSF Context DB Action Class Entity Class Seam Framework JSF Components RegisterActionBean U...
Integrating the Seam Framework <ul><li>Additions... </li></ul><ul><ul><li>EJB Module (jar) </li></ul></ul><ul><ul><ul><li>...
DEMO
faces-config.xml <managed-bean-name> user </... <managed-bean-class>org.ex... 4. ManagedBean userName name password regist...
DEMO
User.java (1 of 2) <ul><li>@Entity </li></ul><ul><li>@Name(&quot;user&quot;)   </li></ul><ul><li>@Scope(ScopeType.Event)  ...
User.java  (2 of 2) <ul><li>public User() {}  </li></ul><ul><li>@Length(min=5, max=15)   </li></ul><ul><li>public String g...
RegisterAction.java  <ul><li>@Stateless  </li></ul><ul><li>@Name(&quot;register&quot;)   </li></ul><ul><li>public class Re...
AGENDA <ul><ul><li>The Java EE 5 Programming Model </li></ul></ul><ul><ul><li>Introduction to Seam </li></ul></ul><ul><ul>...
Seam on GlassFish <ul><ul><li>Add missing JBoss Libraries: </li></ul></ul><ul><ul><ul><li>hibernate-all.jar </li></ul></ul...
DEMO
Summary <ul><li>Hopefully you've learned how to start using the Seam framework in your existing JSF / EJB 3.0 applications...
<ul><li>Repeat these demos yourself by visiting my blog at  http://weblogs.java.net/blog/bleonard </li></ul>
Questions & Answers
Upcoming SlideShare
Loading in...5
×

Os Leonard

794

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
794
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Os Leonard

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

    ×