You are on page 1of 13

IT IMPACT 2005

Struts, JSF, Spring en Hibernate in action


by Ronald Koster
It is not important whether a technology will be
successful or not, it is important to wonder how we
would react if it did...

NLJUD 2005

20 December, 2005

Struts, JSF, Spring en Hibernate in action

For building Java/J2EE application one can choose form a


extensive set of framework. The ones mentioned above are
among the most popular ones. This presentation will attempt to
answer the following questions:
What are the differences and matches of Struts and JSF?
Can they be combined? If so, would you want to?
What is Spring and why is it called the glue framework?
What is Hibernate and why is it great?
How to combine Spring, Hibernate and Session Beans?

NLJUD 2005

20 December, 2005

Struts and JSF 1/4

Struts and JSF are MVCs for building web applications.


JSF is supposed successor of Struts.
+ Managed Beans, EL, Event Driven
- More complex (bigger learning curve), Less documentation, Less transparent.

Tutorial by Marty Hall: http://www.coreservlets.com/JSF-Tutorial


JSF more event driven (like traditional GUIs) than Struts.
Struts: pure request/response driven. JSF: event driven but request/response
approach still available too.

mGBA project: MyFaces (initially RI).


Combining Struts and JSF: No, except for Tiles if you want to.

NLJUD 2005

20 December, 2005

Struts and JSF - 2/4 Struts flow of control

OR
User

Preceding
Action

Action
Servlet

Jsp

invoke ActionX

new: in case of request scope form


fetch: in case of session scope form
OR internal forward

new/fetch

BL Beans

FormX

BLBean =
Business Logic Bean =
Service Bean

reset
populate
new
If target is a JSP, else it
is another Action, in which
case this whole sequence
starts again.

ActionX

execute
business method
store result
target name

display
getters

NLJUD 2005

20 December, 2005

Struts and JSF 3/4 JSF flow of control


(Faces request, Faces response)

Jsp1

Jsp2

new: in case of request scope bean


fetch: in case of sessions scope bean
OR internal forward

Faces
Servlet

User
submit
invoke BeanX.methodY
new/fetch

BL Beans

BeanX and other


Jsp1 beans

populate beans
methodY
business method A (by methodY)
target JSP name
new/fetch
display
getters
getters

NLJUD 2005

20 December, 2005

Other
Jsp2 beans
business method B (by getters)
business method C
(by getters)

Struts and JSF 4/4 - Flow of control comparison

Struts:

JSF:

Model: Forms + BLBeans


Controller: Actions + ActionServlet
View: JSPs

Model: JSF-Beans + BLBeans


Controller: JSF-Beans + FacesServlet
View: JSPs

JSF-Bean can be both Model and Controller. Less strict


enforcing of separation of Model and Controller!
For complex navigational flows it is a good idea to build
your own MVC by differentiating between ControllerBeans
(aka. ActionBeans) and DataBeans.

NLJUD 2005

20 December, 2005

Spring 1/3 - What is Spring

Spring relies heavily on the Inverse of Control (IoC) design


pattern using Dependency Injection.
(Hollywood principle: Dont call us, we will call you).
NB. JSF Managed beans: IoC, very much like Sping!

Provides extremely convenient consistent encapsulation of


virtually all major APIs and frameworks: the glue framework.
Suited for J2SE and J2EE.
J2EE != EJB. Spring has virtually crushed EJBs:

NLJUD 2005

Entity Beans: Dead: use Spring-JDBC and ORM.


Stateless Session Beans: Dead: use Spring, if needed in combination with
load balancer (for clustering) and application server (as JTA framework).
Stateful Session Beans: still alive, but who has ever used them?
Message Driven Beans: still alive, but Spring alternative is getting better.
Ergo: Usually no need for EJBs and EJB container. However, Web/Servlet
container remains to be needed.
20 December, 2005

Spring 2/3 - Properties

Provides Bean lifecycle management (through IoC).


Has its own web MVC.
But integrates well with JSF as well
(see org.springframework.web.jsf.DelegatingVariableResolver).
Encapsulates Hibernate and other Object Relational
Mapping (ORM) frameworks.
Encapsulates JDBC (JDBC problems usually vanish with
Spring-JDBC!).
Encapsulates transaction management and JTA.
Has own AOP framework and encapulates others.
Has security framework (Acegi).
Encapsulates all major Remoting protocols (HttpInvoker,
Hessian, Burlap, SOAP, RMI, EJBs).
Encapsulates JMX.
And more!
NLJUD 2005

20 December, 2005

Spring 3/3 - Usage

Inverse of Control (IoC): Spring managed beans, configured


through XML files, less Java code. IoC is very simple to use and
very powerful.
Allows piece by piece build of J2EE application, starting with a
simple standard edition application. This enables architectural
decisions to be postponed.
Pick what you need. Can easily be combined with all major
APIs and frameworks.
Encapsulates en glues together APIs and other frameworks.

Simply use it! It will make things easier.

NLJUD 2005

20 December, 2005

Hibernate 1/2 - What is Hibernate

Hibernate is an Object Relational Mapping (ORM) framework.


Many other frameworks exist, but Hibernate is probably the
most popular one.
Entity Beans are dead. EJB3 will be like Hibernate.
Any Plain Old Java Object (POJO) can be persisted.
Hibernate is a simple and powerful framework.

NLJUD 2005

20 December, 2005

10

Hibernate 2/2 - Usage

Key features:
1. Define database through hbm.xml files.
2. Generate database create schema script (SQL) and persistent
Java classes (POJOs) representing the database tables from
these hbm.xml files.

NB. The other way around: generate hbm.xml files from an existing da
is possible also.

3. HQL and Hibernate API.

Do use Spring encapsulation.


Hibernate DAOs can be placed behind Session Beans. But
often you dont (see Spring has virtually crushed EJBs).

NLJUD 2005

20 December, 2005

11

Model Architecture

JSPs + Spring / JSF Beans


Spring
MVC / JSF

Spring
Security

Remote Services
Spring Remoting

Service Spring Beans


Spring JTA

<<Service (BL) tier>>

Spring Security

DAO Spring Beans


Spring JDBC

<<Presentation tier>>

<<DAO tier>>

Spring + Hibernate
: my code

NLJUD 2005

20 December, 2005

: framework
12

Links

NLJUD 2005

Struts: http://struts.apache.org
JSF: http://java.sun.com/j2ee/javaserverfaces
MyFaces: http://myfaces.apache.org
Spring: http://www.springframework.org
Hibernate: http://www.hibernate.org

20 December, 2005

13

You might also like