Вы находитесь на странице: 1из 13

Managed Beans

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

Managed Beans JSF allows users to design a complex tree of named POJO 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 depends on their specified scope

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

POJO Beans Application developers have full control over application design Full control gives you great flexibility in your design but it can also expose you to common design errors

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

Bean Names Bean names uniquely identify the Bean within the context 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
<managed-bean> <managed-bean-name> myBeanName </managed-bean-name> <managed-bean-class> org.mycompany.package.ClassName </managed-bean-class> ... </managed-bean>

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 as the 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;

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 or manual invalidation Unique to each user but share across multiple browser tabs

Request
Lifespan duration of an HTTP request received by the server and response sent to client

No Scope
Bean isnt placed into scope

Application Session

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 be destroyed and reinitialized

Flash
Short conversation-style scope that exists for a single view transition including reloads

Custom
Allows developers to implement their own custom scope behavior

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

Bean Scopes Once a Bean is defined in the faces-config.xml or via an Annotation it has a default of request scope Bean scope can be defined explicitly in the faces-config.xml
<managed-bean> <managed-bean-name> myBeanName </managed-bean-name> <managed-bean-class> org.mycompany.package.ClassName </managed-bean-class> <mangaged-bean-scope> application|session|view|request|flash </managed-bean-scope> </managed-bean>

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 scope and so on until you get the appropriate results Bean scoping is a fine balance between memory usage and a rich user experience. In some instances, it can be difficult to have both

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;

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

Bean Lifecycle JSF implementations running in a Java EE 5 compliant container have access to two other annotations:
@PostConstruct @PreDestroy

Methods on managed beans can be annotated:


@PostConstruct Public void myMethod(){ // initialization logic. }

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 the container management
Bean scope plays an important role as to when this method is called Can be handy for proactively cleaning up a memory foot print or un-registering a class from a listener

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

End Presentation

ICESOFT TECHNOLOGIES INC.

www.icefaces.org

Вам также может понравиться