click to edit title
Java EE
JSF
MVC Architecture
Model
View
Controller
JSF Engine
CDI Bean
Built using Facelet declaration language
xhtml page
Tags
Composite component based
Reusable snippet of code
Has various elements attached (e.g. bean validation)
Data bindings
3rd Party Pluggable component libraries
PrimeFaces
Apache MyFaces
ICEFaces
Navigation
Replaces values
The bound model
The bound view
Ajax
Supported out of the box
<f:ajax /> e.g. <f:ajax event="click" />
Facelet declaration language
HTML Tags (Not required - Can use HTML tags directly)
<h:form>
<h:inputText>
<h:commandButton>
<h:selectOneRadio>
<h:inputTextArea>
<h:outputText>
<h:commandLink>
Facelets Tags
<ui:composition>
<ui:insert>
<ui:repeat>
<ui:fragment>
<ui:debug>
<ui:decorate>
<ui:param>
<ui:define>
Repeat a section of code (i.e. loop)
Start a composite definition
Core Tags
<f:convert>
<f:convertDateTime>
<f:lengthvalidate>
<f:ajax>
<f:loadBundle>
<f:selectItem>
<f:actionListener>
CDI Scopes
for
Types
Dependent (default)
Not share between clients
Created and destroyed with the instance it belongs to
Determines which client refers to which instance of the bean
The scope of a bean determines the bean's lifecycle
ViewScope (@ViewScoped
)
FlowScope (@FlowScoped
)
FlowDefinition (@FlowDefinition
)
Share the same lifecycle as the view which initially referenced them
Connect logically related pages together
Useful for views that make use of AJAX
Connect logically related pages together
Allow the bean lifecycle to be defined for a collection of views
Allow the bean lifecycle to be defined for a collection of views
JSF engine will find the view
Pass view name to the action of component
CDI
Annotations
@Produces
Mark the return value an "injectable" bean
@Inject
Mark the injection point
@Disposes
@Stereotype
Identifies the disposed parameter of a disposer method, which allows the application to perform customized clean-up of an object returned by a producer method/field
Combines a number of annotations that are repeatedly used in different places
@Named
Enables a bean to be accessible from EL
Injections
Inject by type
When type is not sufficient, use @Qualifier
Scopes
@RequestScoped
@ViewScoped
@FlowScoped
@Transaction
@SessionsScoped
@ConversationScoped
@Dependent
@CustomScoped
Bean lives as long as the HTTP request-response lives.
created upon a HTTP request
gets destroyed when the HTTP response associated with the HTTP request is finished
Bean lives as long as the user is interacting with the same JSF view in the browser window/tab
It gets created upon a HTTP request
gets destroyed once the user postbacks to a different view
Bean lives as long as the HTTP session lives
gets created upon the first HTTP request involving this bean in the session
gets destroyed when the HTTP session is invalidated
@ApplicationScoped
gets created upon the first HTTP request involving this bean in the application
Bean lives as long as the web application lives
gets destroyed when the web application shuts down.
Advanced scope. Usually used when extending CDI framework
Default scope
Interceptors
Decorator
Event Bus
Lightweight
Enables messages to be passed through the system in a very decoupled way
@Observes
Mark the event (e.g. a method parameter) to observe
Beans don't require to be marked with @Named
annotation to be accessible in other part of the system. It is, however, required for them to be accessed in JSF
<f:viewParam>
Get parameter from query string
Expression language
#{not empty bean.attributeName}
EJB3
Attributes
Encapsulate business logic
Execute in container
Always run within a transaction
Container console shows data (e.g. response time) related to EJB currently running
Types
Session Bean
@Stateless
@Stateful
@Singleton
Message Driven Bean
Functions as an extension of the client - it holds distinct client client-related state data
1-1 relationship between client and bean e.g. 100 client connection means 100 instances of stateful bean in memory
Used when client state is required to be held
Can be shared among multiple clients
Can be shared among multiple clients
Attributes
Don't need to worry about how a message is delivered or consumed
It is just about messaging handling
Unlike Session bean, it cannot be accessed directly by client
Can only be interested with via message
Access modes
Specified via annotations
Types
Local (@Local
)
only access within the same container
Thread safety - each EJB instance runs in a single thread context
Remote (@Remote
)
Accessible from external applications
Web service
Accessible with a client using web service
Annotations
@Lock
Concurrency control
@AccessTimeout
@Transactional
@PermitAll
Every security roles are allowed to invoke the annotated method
@RolesAllowed
Specifies the security roles required to access the method
@Asychronous
Act as queue based
@Schedule
Execute using cron-style time schedule
@TransactionAttribute
Extension of the messaging system, representing endpoint functionality
Roles
Destination
Queue
Topic
Publisher/Sender
Subscriber/receiver/listener
Message
Has a payload that contains the request that needs to be passed
Types
TextMessage
ByteMessage
StreamMessage
ObjectMessage
MapMessage
Setup
Point-to-Point
Publisher/Receiver
Destinations are known as queue
Messages are delivered to a single receiver
Queues hold messages until it is processed by receiver
Destinations are known as topics
Messages are delivered (broadcast) to all registered subscribers
Topics don't hold messages
JPA
Data Modelling
Implementations
Hibernate
Toplink
Eclipselink
@Entity
@Id
@Generated
@Embeded
@OneToOne
@OneToMany
@ManyToOne
@ManyToMany
@Table
@Embeddable
@Column
@Enumerated
@Temperal
@Transient
@JoinColumn
@JoinTable
Bean validation
Annotations
Overview
Can validate
Bean's attribute
Parameter
Return value
Can be triggered by
JSF
JPA
CDI
JAX-RS
ValidatorFactory
@AssertTrue
@DecimalMax
Used to validate unmanaged POJO
@Future
@Max(100)
@NotNull
Used on boolean field
Used on date field
Exception is thrown if validation fails
@Pattern
Regular expression
@Size
Size of a field
@Digits