JSF – Managed Beans

  • Managed Bean is a regular Java Bean class registered with JSF. In other words, Managed Beans is a java bean managed by JSF framework.

  • The managed bean contains the getter and setter methods, business logic or even a backing bean (a bean contains all the HTML form value).

  • Managed beans works as Model for UI component.

  • Managed Bean can be accessed from JSF page.

  • In JSF 1.2,a managed bean had to register it in JSF configuration file such as faces-config.xml.

  • From JSF 2.0 onwards, Managed beans can be easily registered using annotations. This approach keeps beans and there registration at one place and it becomes easier to manage.

Using Annotation

@ManagedBean(name = "helloWorld", eager = true)
@RequestScoped
public class HelloWorld {
	
   @ManagedProperty(value="#{message}")
   private Message message;
   ...
}

@ManagedBean Annotation

@ManagedBean marks a bean to be a managed bean with the name specified in name attribute. If the name attribute is not specified, then the managed bean name will default to class name portion of the fully qualified class name. In our case it would be helloWorld.

Another important attribute is eager. If eager=”true” then managed bean is created before it is requested for the first time otherwise “lazy” initialization is used in which bean will be created only when it is requested.

Scope Annotations

Scope annotations set the scope into which the managed bean will be placed. If scope is not specified then bean will default to request scope. Each scope is briefly discussed below.

image

 

@ManagedProperty Annotation

JSF is a simple static Dependency Injection(DI) framework.Using @ManagedProperty annotation a managed bean’s property can be injected in another managed bean.

JSF : Architecture

 

A JSF application is similar to any other Java technology-based web application; it runs in a Java servlet container, and contains

  • JavaBeans components as models containing application-specific functionality and data

  • A custom tag library for representing event handlers and validators

  • A custom tag library for rendering UI components

  • UI components represented as stateful objects on the server

  • Server-side helper classes

  • Validators, event handlers, and navigation handlers

  • Application configuration resource file for configuring application resources

JSF Architecture

There are controllers which can be used to perform user actions.UI can be created by web page authors and the business logic can be utilized by managed beans.

JSF provides several mechanisms for rendering an individual component. It is upto the web page designer to pick the desired representation, and the application developer doesn’t need to know which mechanism was used to render a JSF UI component.

JSF : MVC Design Pattern in JSF

JSF technology is a framework for developing, building server side User Interface Components and using them in a web application.JSF technology is based on the Model View Controller (MVC) architecture for separating logic from presentation.

MVC Design Pattern in JSF

MVC design pattern designs an application using three separate modules:

image

Purpose of MVC design pattern is to separate model and presentation to enable developers to set focus on their core skills and collaborate more clearly.

Web Designers have to concentrate only on view layer rather than model and controller layer. Developers can change the code for model and typically need not to change view layer.Controllers are used to process user actions. In this process layer model and views may be changed.

JSF : Benefits of JSF

JSF reduces the effort in creating and maintaining applications which will run on a Java application server and will render application UI on to a target client.JSF facilitates Web application development by

  • proving reusable UI components

  • making easy data transfer between UI components

  • managing UI state across multiple server requests

  • enabling implementation of custom components

  • wiring client side event to server side application code

JSF : What is Java Server Faces (JSF)

A complete web application requires a web framework to generate HTML pages, receive user input, and manage the navigation flow. The consensus among most web developers is that a Model-View-Controller (MVC) architecture is the best for web frameworks. In an MVC framework, the controller takes user input and decides which view to show next; The view renders the HTML page for the browser; The model encapsulates data captured from the web form and to be displayed on the web page.

20110510-jsf-logo.tiff

Shield developers from the messy HTML / JavaScript / CSS details

Enterprise Java developers have long embraced ORM solutions, which automatically generate relational database access code from the data model component in the application. JSF is the “ORM solution for the web layer”. It automatically generates HTML / JavaScript / CSS code from well formed UI components.

A JSF application primarily contains two types of components — both are easy to use and conforming to the POJO philosophy.

  • A JSF page is composed of XML tags. Each tag element corresponds to a UI component in JSF. As a web developer, you do not need to deal with the HTML markup or JavaScript details, as they are all generated from the JSF component tags. Since each UI component is self-contained and has well-defined behavior (i.e., it understand how to render itself and how to handle its data), JSF provides a very POJO-like experience for developing web UI.

  • Dynamic data on JSF pages and forms are modeled in POJOs known as JSF backing beans. The backing bean lifecycle is managed by the JSF server. For instance, a backing bean can have a session scope to track a user session. A backing bean can be dependency injected into another bean via an XML configuration file, and it can be injected into a JSF UI component via the JSF Expression Language (EL). The backing bean also provides the integration points between the web UI and the EJB3 business components.

The componentized UI and POJO data model make it easy to support JSF in IDE tools. In fact, many Java IDEs now support drag-and-drop UI designers for JSF. The JSF component model also allows third party vendors to develop and sell reusable UI component libraries. Those component libraries make it easy for server-side developers to take advantage of the latest browser technology without the need to mess with JavaScripts and such.

The JSF request model is powerful and easy to extend. Technologies like Facelets and Seam have been developed on top of JSF, providing even richer development environments. While competing frameworks all have nice features, JSF seems to be the easiest to extend. We expect even more innovative extensions in the near future, clearly making JSF the framework of choice for most EE projects.

As being a MVC (Model-View-Controller) framework, JSF provides the FacesServlet as the sole request-response Controller. It takes all the standard and tedious HTTP request/response work from your hands, such as gathering user input, validating/converting them, putting them in model objects, invoking actions and rendering the response. This way you end up with basically a JSP or Facelets (XHTML) page for View and a Javabean class as Model. The JSF components are been used to bind the view with the model (such as your ASP.NET web control does) and the FacesServlet uses the JSF component tree to do all the work.

JSF is a component based MVC framework which is built on top of the Servlet API and providescomponents in flavor of taglibs which can be used in JSP or any other Java based view technology such as Facelets.

There are many web frameworks available. JavaServer Faces (JSF), is the standard Java EE web framework . You will find it similar/alternative if you might be knowing about any of these below web frame work:
Struts
WebWork (Struts Action2)
Shale (Struts-JSF)
SpringMVC
Wicket
Tapestry
Stripes
Flex
Open Laszlo

 

Botton line : JSF is the “ORM solution for the web layer ie Java based web UI framework.ā€