Struts, JSF, Spring en Hibernate in Action

  • Upload
    javier

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    1/13

    1NLJUD 2005 20 December, 2005

    IT IMPACT 2005

    It is not important whether a technology will besuccessful or not, it is important to wonder how we

    would react if it did...

    Struts, JSF, Spring en Hibernate in action

    by Ronald Koster

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    2/13

    2NLJUD 2005 20 December, 2005

    Struts, JSF, Spring en Hibernate in action

    For building Java/J2EE application one can choose form aextensive set of framework. The ones mentioned above are

    among the most popular ones. This presentation will attempt toanswer 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?

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    3/13

    3NLJUD 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/responseapproach still available too.

    mGBA project: MyFaces (initially RI). Combining Struts and JSF: No, except for Tiles if you want to.

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    4/13

    4NLJUD 2005 20 December, 2005

    Struts and JSF - 2/4 Struts flow of control

    Jsp

    invoke ActionX

    Action

    Servlet

    FormX

    ActionX

    BL BeansUser

    business method

    reset

    execute

    target name

    new/fetch

    display

    new

    populate

    store result

    getters

    Preceding

    ActionOR

    new: in case of request scope form

    fetch: in case of session scope form

    OR internal forward

    If target is a JSP, else it

    is another Action, in which

    case this whole sequence

    starts again.

    BLBean =

    Business Logic Bean =

    Service Bean

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    5/13

    5NLJUD 2005 20 December, 2005

    Struts and JSF 3/4 JSF flow of control(Faces request, Faces response)

    Jsp1 Jsp2

    submitinvoke BeanX.methodY

    Faces

    Servlet

    BeanX and other

    Jsp1 beans

    BL Beans

    User

    target JSP name

    new/fetch

    display

    populate beans

    methodY

    getters

    business method A (by methodY)

    business method B (by getters)

    new: in case of request scope bean

    fetch: in case of sessions scope bean

    OR internal forward

    Other

    Jsp2 beans

    new/fetch

    gettersbusiness method C

    (by getters)

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    6/13

    6NLJUD 2005 20 December, 2005

    Struts and JSF 4/4 - Flow of control comparison

    Struts:Model: Forms + BLBeans

    Controller: Actions + ActionServletView: JSPs

    JSF:Model: JSF-Beans + BLBeans

    Controller: JSF-Beans + FacesServletView: JSPs

    JSF-Bean can be both Model and Controller. Less strictenforcing of separation of Model and Controller!

    For complex navigational flows it is a good idea to buildyour own MVC by differentiating between ControllerBeans

    (aka. ActionBeans) and DataBeans.

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    7/13

    7NLJUD 2005 20 December, 2005

    Spring 1/3 - What is Spring

    Spring relies heavily on the Inverse of Control (IoC) designpattern 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 ofvirtually all major APIs and frameworks: the glueframework.

    Suited for J2SE and J2EE. J2EE != EJB. Spring has virtually crushed EJBs:

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

    Stateless Session Beans: Dead: use Spring, if needed in combination withload 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.

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    8/13

    8NLJUD 2005 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 RelationalMapping (ORM) frameworks.

    Encapsulates JDBC (JDBC problems usually vanishwith

    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!

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    9/13

    9NLJUD 2005 20 December, 2005

    Spring 3/3 - Usage

    Inverse of Control (IoC): Spring managed beans, configuredthrough XML files, less Java code. IoC is very simple to use andvery powerful.

    Allows piece by piece build of J2EE application, starting with asimple standard edition application. This enables architecturaldecisions to be postponed.

    Pick what you need. Can easily be combined with all majorAPIs and frameworks.

    Encapsulates en glues together APIs and other frameworks.

    Simply use it! It will make things easier.

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    10/13

    10NLJUD 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 themost popular one.

    Entity Beans are dead. EJB3 will be like Hibernate.

    Any Plain Old Java Object (POJO) can be persisted.

    Hibernate is a simple andpowerful framework.

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    11/13

    11NLJUD 2005 20 December, 2005

    Hibernate 2/2 - Usage

    Key features:

    1. Define database through hbm.xml files.2. Generate database create schema script (SQL) andpersistent

    Java classes (POJOs) representing the database tables from

    these hbm.xml files.NB. The other way around: generate hbm.xml files from an existiis possible also.

    3. HQL and Hibernate API.

    Do use Spring encapsulation.

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

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    12/13

    12NLJUD 2005 20 December, 2005

    Model Architecture

    SpringMVC / JSF

    Spring Remoting

    Service Spring Beans

    Spring JDBC Spring + Hibernate

    Spring JTA

    SpringSecurity

    Spring Security

    JSPs + Spring / JSF Beans Remote Services

    DAO Spring Beans

    : framework: my code

  • 7/23/2019 Struts, JSF, Spring en Hibernate in Action

    13/13

    13NLJUD 2005 20 December, 2005

    Links

    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