Making Distinctions Between Different Kinds of JSF Managed-Beans

http://java.dzone.com/print/9925

Published on Javalobby (http://java.dzone.com) Making Distinctions Between Different Kinds of JSF Managed-Beans
By ngriffin7a Created 2009/04/24 - 10:18am

JSF has a simple Inversion-of-Control (IoC) container [1] called the JSF Managed Bean Facility [2] (MBF). Although it has a verbose XML syntax, and is not as robust as the Spring BeanFactory [3], PicoContainer [4], or the JBoss Microcontainer [5], the MBF does have the basics of an IoC container, and offers features like dependency injection [6]. When a POJO is managed by the JSF MBF, it is typically referred to as a managed-bean. But if you're going to create a maintainable JSF webapp/portlet, it is necessary to distinguish between different kinds of managed-beans. This practice will also preserve the clean separation of concerns [7] that JSF provides by implementing the Model-View-Controller [8] (MVC) design pattern:

Managed-Bean Type Model Managed-Bean

Nickname model-bean

Typical Scope session

Description: This type of managed-bean participates in the "Model" concern of the MVC design pattern. When you see the word "model" -- think DATA. A JSF model-bean should be a POJO that follows the JavaBean design pattern with getters/setters encapsulating properties. The most common use case for a model bean is to be a database entity, or to simply represent a set of rows from the result set of a database query. Backing Managed-Bean backing-bean request

Description: This type of managed-bean participates in the "View" concern of the MVC design pattern. The purpose of a backing-bean is to support UI logic, and has a 1::1 relationship with a JSF view, or a JSF form in a Facelet composition. Although it typically has JavaBean-style properties with associated getters/setters, these are properties of the View -- not of the underlying application data model. JSF backing-beans may also have JSF actionListener and valueChangeListener methods. Controller Managed-Bean controller-bean request

Description: This type of managed-bean participates in the "Controller" concern of the MVC design pattern. The purpose of a controller bean is to execute some kind of business logic and return a navigation outcome to the JSF navigation-handler. JSF controller-beans typically have JSF action methods (and not actionListener methods). Support Managed-Bean support-bean session / application

Description: This type of bean "supports" one or more views in the "View" concern of the MVC design pattern. The typical use case is supplying an ArrayList<SelectItem> to JSF h:selectOneMenu drop-down lists that appear in more than one JSF view. If the data in the dropdown lists is particular to the user, then the bean would be kept in session scope. However, if the data applies to all users (such as a dropdown lists of provinces), then the bean would be kept in application scope, so that it can be cached for all users. Utility Managed-Bean utility-bean application

Description: This type of bean provides some type of "utility" function to one or more JSF views. A good example of this might be a FileUpload bean that can be reused in multiple web applications.

Now... One of the main benefits in making fine distinctions like this is loose coupling [9]. What's that you ask? Well let's first take a look at an example of tight coupling, where MVC concerns can be smashed/confused into a single managed-bean:

public class ModelAndBackingAndControllerBean { private String fullName; // model-bean property private boolean privacyRendered; // backing-bean property // model-bean getter

1 of 3

20/09/2010 8:29 PM

Additionally.println("fullName=" + fullName). } } The problem here is that the bean would have to be kept in session scope because of the model-bean property. } // backing-bean setter public void setPrivacyRendered(boolean privacyRendered) { this.fullName = fullName. what if we wanted to do some unit testing with mock model data? Can't do it.println("fullName=" + getModelBean().com/print/9925 public String getFullName() { return fullName.getFullName()).Making Distinctions Between Different Kinds of JSF Managed-Beans http://java. } public void togglePrivacySection(ActionEvent actionEvent) { privacyRendered = !privacyRendered.out. public void setPrivacyRendered(boolean privacyRendered) { this. } } public class BackingBean { private boolean privacyRendered. } // backing-bean actionListener for UI support logic public void togglePrivacySection(ActionEvent actionEvent) { privacyRendered = !privacyRendered. public ModelBean getModelBean() { return modelBean. } // model-bean setter public void setFullName(String fullName) { this.dzone. } public void setModelBean(ModelBean modelBean) { // Dependency injected from the JSF managed-bean facility this. } // backing-bean getter public boolean isPrivacyRendered() { return privacyRendered. public void setFullName(String fullName) { this.modelBean = modelBean. } public String submit() { System. } public boolean isPrivacyRendered() { return privacyRendered. return "success". return "success".privacyRendered = privacyRendered.out. } // controller-bean business logic public String submit() { System. and to promote loose coupling. } public String getFullName() { return fullName. we would have three separate Java classes: public class ModelBean { private String fullName.privacyRendered = privacyRendered. } } 2 of 3 20/09/2010 8:29 PM . } } public class ControllerBean { private ModelBean modelBean.fullName = fullName. So in order to fix these problems.

com/xml/ns/javaee http://java. thus saving memory resources on the server. and the backing-bean and controller-bean can be kept in request scope.w3.BackingBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>controllerBean</managed-bean-name> <managed-bean-class>myproject.com/xml/ns/javaee/web-facesconfig_1_2.wikipedia.org/jbossmc/ [6] http://en.sun.oracle.dzone.com/technology/tech/java/newsletter/articles/jsf_pojo/index.wikipedia.org/ [10] Source URL: http://java.xml example.org/wiki/Model-view-controller [9] http://en.org/wiki/Separation_of_concerns [8] http://en. where the #{modelBean} Expression Language (EL) binding is used: <?xml version="1.org/wiki/Inversion_of_control [2] http://www.org/ [5] http://www.org/wiki/Dependency_injection [7] http://en.Making Distinctions Between Different Kinds of JSF Managed-Beans http://java. we can use the dependency injection features of the JSF MBF in order to inject the model-bean into the controller-bean.picocontainer.org/wiki/Loose_coupling [10] http://blog.wikipedia.jboss.dzone.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.xsd" version="1.wikipedia. Finally.wikipedia.com/print/9925 Now that the beans are found in different classes. The model-bean can be kept in session scope.sun.sun.com/xml/ns/javaee" xmlns:xsi="http://www.2"> <managed-bean> <managed-bean-name>modelBean</managed-bean-name> <managed-bean-class>myproject.icefaces.ModelBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> <managed-bean> <managed-bean-name>backingBean</managed-bean-name> <managed-bean-class>myproject.org/wiki/Spring_Framework#Inversion_of_Control_container [4] http://www.com/articles/making-distinctions-between Links: [1] http://en.html [3] http://en.org/ 3 of 3 20/09/2010 8:29 PM . This can be seen in the following WEB-INF/faces-config.0" encoding="UTF-8"?> <faces-config xmlns="http://java.icefaces. they can all be kept in their appropriate scopes.wikipedia.ControllerBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> <managed-property> <property-name>modelBean</property-name> <value>#{modelBean}</value> </managed-property> </managed-bean> </faces-config> From http://blog.

Sign up to vote on this title
UsefulNot useful