1.3.0 Lecture Managed Beans

download 1.3.0 Lecture Managed Beans

of 13

Transcript of 1.3.0 Lecture Managed Beans

  • 8/14/2019 1.3.0 Lecture Managed Beans

    1/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Managed Beans

  • 8/14/2019 1.3.0 Lecture Managed Beans

    2/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Managed Beans

    JSF allows users to design a complex tree of namedPOJO Beans

    Beans have a predetermined lifespan known as scope

    Beans can be defined using:

    Managed Bean Creation facility (faces-config.xml)

    Java EE 5 annotations

    Managed Beans also have a lifecycle which dependson their specified scope

  • 8/14/2019 1.3.0 Lecture Managed Beans

    3/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    POJO Beans

    Application developers have full control overapplication design

    Full control gives you great flexibility in your designbut it can also expose you to common design errors

  • 8/14/2019 1.3.0 Lecture Managed Beans

    4/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Names

    Bean names uniquely identify the Bean within thecontext of the application

    Follow the same naming conventions as Java classes,mixed case starting with lower case

    Bean names can be defined in faces-config.xml

    myBeanName org.mycompany.package.ClassName ...

  • 8/14/2019 1.3.0 Lecture Managed Beans

    5/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Names

    JSF 2 introduces the @ManagedBean annotation

    Names can be specified with the name attribute@ManagedBean(name=someName)

    If a name is not specified the class name is used asthe Bean name, mixed case starting with lower case

    The eager attribute can be used to insure that a bean

    is loaded in a non-lazy fashion@ManagedBean(name=someName, eager=true)

    Note: import javax.faces.bean. ManagedBean;

  • 8/14/2019 1.3.0 Lecture Managed Beans

    6/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Scopes

    JSF 1.x originally defined four scope types:

    Application

    Lifespan continues as long as the web application is deployed

    Session

    Lifespan of the HttpSession, destroyed by session timeout ormanual invalidation

    Unique to each user but share across multiple browser tabs

    Request

    Lifespan duration of an HTTP request received by the serverand response sent to client

    No Scope

    Bean isnt placed into scope

    Application

    Session

  • 8/14/2019 1.3.0 Lecture Managed Beans

    7/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Scopes

    JSF 2 introduces three new scopes:

    View

    Bean lasts the duration of the view

    Page navigation or page refreshes cause the Bean to bedestroyed and reinitialized

    Flash

    Short conversation-style scope that exists for a single view

    transition including reloads

    Custom

    Allows developers to implement their own custom scopebehavior

  • 8/14/2019 1.3.0 Lecture Managed Beans

    8/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Scopes

    Once a Bean is defined in the faces-config.xml or viaan Annotation it has a default of request scope

    Bean scope can be defined explicitly in thefaces-config.xml

    myBeanName org.mycompany.package.ClassName

    application|session|view|request|flash

  • 8/14/2019 1.3.0 Lecture Managed Beans

    9/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Scopes

    How do I know which scope to use?

    Try to avoid jamming all state in to the Session scope.

    Start of with request scoped beans

    If you need the Bean to live longer move it to view scopeand so on until you get the appropriate results

    Bean scoping is a fine balance between memory usage anda rich user experience. In some instances, it can be difficultto have both

  • 8/14/2019 1.3.0 Lecture Managed Beans

    10/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Scopes

    Scopes can also be defined with annotations

    @ApplicationScoped

    @SessionScoped

    @ViewScoped

    @RequestScoped

    @CustomScoped(value="#{someMap}")

    @NoneScoped

    Note: make sure to import:javax.faces.bean.Xscoped;

  • 8/14/2019 1.3.0 Lecture Managed Beans

    11/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Lifecycle

    JSF implementations running in a Java EE 5compliant container have access to two other

    annotations: @PostConstruct

    @PreDestroy

    Methods on managed beans can be annotated:

    @PostConstructPublic void myMethod(){ // initialization logic.}

  • 8/14/2019 1.3.0 Lecture Managed Beans

    12/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    Bean Lifecycle

    When the bean is initialized the method binding for@PostConstruct is called immediately after the Class is

    initialized Convenient method for initialization data at construction

    time

    @PreDestroy is called just before the Bean is removed from thecontainer management

    Bean scope plays an important role as to when this method iscalled

    Can be handy for proactively cleaning up a memory foot printor un-registering a class from a listener

  • 8/14/2019 1.3.0 Lecture Managed Beans

    13/13

    ICESOFT TECHNOLOGIES INC. www.icefaces.org

    End Presentation