Title: Refactoring to Seam Web Beans
1(No Transcript)
2Refactoring to Seam (Web Beans)
- (Delete this element)
- Place your picture here
Brian Leonard Java Technology Evangelist Sun
Microsystems, Inc.
- (Delete this element)
- If applicable, place your company logo
3Overall Presentation Goal
- Learn enough about Seam to begin using it
tomorrow
4Speakers Qualifications
- Brian Leonard began working with enterprise Java
in 1997 at NetDynamics. Today he's a Java
Technology Evangelist at Sun Microsystems
5Question
- Are Java Server Faces, which are now part of the
Java EE specification, EJB aware?
6Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
7Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
8Java EE 5 Goal
- Make it easier to develop Java EE applications
- Especially when first getting started with Java EE
9How Was It Made Easier?
- Then
- Deployment descriptors
- Required container interfaces
- JNDI Lookups
- Configuration files and command line options
- No supported UI framework
- Now
- Java language annotations
- Plain Old Java Objects (POJOs)
- Dependency injection
- More and better defaults
- Java Server Faces (JSF)
10Question
- Are Java Server Faces, which are now part of the
Java EE specification, EJB aware?
11How Much Easier Is It?
1 Source Raghu Kodali, Oracle
12Annotations Everywhere
- For defining and using web services
- _at_WebService
- To greatly simplify EJB development
- _at_Stateless, _at_Stateful, _at_MessageDriven
- To map Java classes to databases
- _at_Entity, _at_Table, _at_Column
- To specify external dependencies
- _at_Resource
- Reduces need for deployment descriptors and JNDI
lookups
13For Example - J2EE 1.4 Web Service
? j2ee' version'1.1'
HelloService
WEB-INF/wsdl/HelloService.wsdlsdl-file
WEB-INF/HelloService-mapping.xml
xmlnswsdl-port_ns'urnHelloService/wsdl'
HelloServicename wsdl-port_nsHelloServiceSE
IPort e endpoint.HelloServiceSEInt-interface
WSServlet_HelloService
bservices encoding'UTF-8' ? xmlns'http//java.sun.com/xml/ns/jax-rpc/ri/confi
g' targetNamespace'urnHelloService/wsdl'
typeNamespace'urnHelloService/types'
packageName'endpoint' name'endpoint.HelloServiceSEI'
servantName'endpoint.HelloServiceImpl'
package endpoint import java.rmi. public
class HelloServiceImpl implements
HelloServiceSEI public String sayHello(String
param) throws java.rmi.RemoteException
return Hello param package
endpoint import java.rmi. public interface
HelloServiceSEI extends java.rmi.Remote
public String sayHello(String param) throws
java.rmi.RemoteException
14Java EE 5 Web Service
package endpoint import javax.jws.WebService _at_
WebService public class Hello public String
sayHello(String param) return Hello
param
15Java EE 5 Programming Model
Registration Application
Entity Class
Managed Bean
16DEMO
17Java EE 5 Programming Model
Registration Application
Entity Class
Managed Bean
DB
User
RegisterActionBean
ManagedBean
JSF Components
Action Class
JSF Context
18register.jsp JSF In Action
Username id"userName"
value"user.username"
required"true" minimum"5"
maximum"15"/ Real Name
value"user.name"
required"true" Password
value"user.password"
required"true" minimum"5"
maximum"15"/ id"registerCommand"
type"submit" value"Register"
action"user.register"/
A JSF Validator
19register.jsp BackingBean
- ...
- Username
- id"userName"
- value"user.username"
- ...
- Real Name
- id"name"
- value"user.name"
- ...
- Password
- id"password"
- value"user.password"
- ...
- id"registerCommand"
- type"submit" value"Register"
user
ManagedBean username name password register
20Managed Beans Configuration
- id"userName"
- value"user.username"
faces-config.xml
- ...
-
- user
-
- org.examples.jsf.ManagedBean
-
- requestscope
-
- ...
21Managed Bean
public class ManagedBean private
String username private String name
private String password public String
getUsername() return username Public
String setUsernaame(String username)
this.usernameusername ... private
RegisterActionLocal registerActionBean
private InitialContext ctx try
ctx new InitialContext()
registerActionBean (RegisterActionLocal)
ctx.lookup("registration/RegisterAc
tionBean/local") catch
(NamingException ex)
ex.printStackTrace()
public String register() return
registerActionBean.register(username, name,
password)
22EJB 3.0
- Dramatic simplification of all bean types
- Regular Java classes (POJO)
- _at_Stateless, _at_Stateful, _at_MessageDriven annotations
- Use standard interface inheritance
- Dependency injection
- Instead of using JNDI API to location components
and resources, let the container fetch them for
you. - Interceptors
- Entity Beans (CMP) replaced with JPA
23Java Persistence API (JPA)
- Single persistence API for Java EE AND Java SE
- Much simpler than EJB CMP
- At least three implementations (all open source)
- Oracle GlassFish/TopLink Essentials
- JBoss Hibernate
- BEA Kodo/OpenJPA
- Configured via persistence.xml
- Feedback is overwhelmingly positive
24JPA 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 (TopLink, Hibernate, etc.) - Annotations define how to map objects to tables
- _at_Entity marks a regular Java class as an entity.
- Class attributes map to table columns. Can be
customized with _at_Column. - Manage relationships _at_OneToMany, ...
25Our Entity Bean
- _at_Entity
- _at_Table(name"users")
- public class User implements Serializable
- _at_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...
26JPA Entity Manager
- EntityManager stores/retrieves data
- Inject EntityManager
- _at_PersistenceContext private EntityManager em
- Create an instance of the entity
- User u new User(params)
- Use EntityManager 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)
27Our Action Bean
- _at_Stateless
- public class RegisterAction implements Register
- _at_PersistenceContext
- private EntityManager em
-
- public String register(String username, String
name, String
password) - List existing em.createQuery("select
username - from User where usernameusername")
- .setParameter("username",username)
- .getResultList()
- if (existing.size()0) // Create a new
user - User user new User(username, name,
password) - em.persist(user)
- return "success"
- else
- FacesContext facesContext
FacesContext.getCurrentInstan
ce() - FacesMessage message new
FacesMessage(username "
already exists") - facesContext.addMessage(null, message)
- return null
28Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
29JBoss Seam
- RAD programming model for data-driven
applications without sacrificing the full power
of Java EE 5 - Framework for integrating JSF and EJB3 component
models. - Bridge web-tier and EJB tier session contexts
- Enable EJB 3.0 components to be used as JSF
managed beans. - Prototype for JSR 299 Web Beans
30JBoss Seam
Some 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.
31Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
32Seam Registration Application
Entity Class
DB
User
Seam Framework
RegisterActionBean
JSF Components
Action Class
JSF Context
33Integrating The Seam Framework
Additions...
- EJB Module (Jar)
- Include jboss-seam.jar
- seam.properties
- Web Module (war)
- faces-config.xml
- SeamPhaseListener
- web.xml
- JndiPattern
- SeamListener
34DEMO
35Eliminating Your ManagedBean
1.
_at_Name("use r") _at_Scope(ScopeType.EVENT)
_at_Name("register")
2.
RegisterActionBean
User
user em
username name password
register
getters setters
" ... ster"/ ...
faces-config.xml useraged-bean-classorg.ex...
4.
ManagedBean
userName name password
register
36DEMO
37User.java (1 of 2)
- _at_Entity
- _at_Name("user")
- _at_Scope(ScopeType.Event)
- _at_Table(name"users")
- 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
- ...
38User.java (2 of 2)
- public User()
-
- _at_Length(min5, max15)
- public String getPassword()
- return password
-
-
- public String getName()
- return name
-
-
- _at_Length(min5, max15)
- public String getUsername()
- return username
-
39RegisterAction.java
- _at_Stateless
- _at_Name("register")
- public class RegisterAction implements Register
- _at_In
- private User user
- _at_PersistenceContext
- private EntityManager em
-
- public String register()
- List existing em.createQuery("select
username - from User where usernameusername")
- .setParameter("username",user.getUsername
()) - .getResultList()
- if (existing.size()0)
- em.persist(user)
- return "success"
- else
- FacesMessages.instance().add("User
- user.username already exists")
40Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
41Seam on GlassFish
- Add missing JBoss Libraries
- hibernate-all.jar
- thirdparty-all.jar
- Change Persistence Unit to TopLink
- Update web.xml
- JndiPattern javacomp/env/
- Delete MyFaces context listener
- RegisterActionEJB reference
42DEMO
43Agenda
- (Brief) Introduction to Java EE 5
- JSF 1.2
- EJB 3.0
- JPA
- Interceptors
- Introduction to Seam
- Refactor to use the Seam Framework
- Seam Portability
- Summary and Resources
44Summary
- 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.
45- Repeat these demos yourself by visiting my blog
at http//weblogs.java.net/blog/bleonard
46QA
47- Thank you for your attention!