O'Reilly logo

JavaServer Faces by Hans Bergsten

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Binding Components to Model Properties

In Example 6-1, the components save their current value and redisplay it when the page is reloaded. That’s great, but in order for the backend code to get the values, it would need to locate the corresponding user interface components and ask them for their values. That would introduce an undesirable dependency between the user interface and the backend code.

To solve this problem, JSF lets you bind components directly to JavaBeans properties of any application class as well as to elements of an array or java.util.List, or java.util.Map [1] entry. When the component is rendered, it reads the property or element value and renders it; when a request is processed, it sets the property or element value to the current component value. This way, the backend code doesn’t need to know anything about the user interface—it just works with the properties of the application model classes.

A JSF application typically uses two types of classes with JavaBeans properties: the real application beans representing application concepts and what I call “glue beans,” which sit between the real application beans and the user interface. Others call the latter a “backing bean” or “code-behind file,” but it all means the same thing.

The Report class introduced in Chapter 5 is an example of a real application bean. The ReportHandler class is an example of a “glue bean,” partly shown in Example 6-2.

Example 6-2. Parts of the ReportHandler class
package com.mycompany.expense; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required