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.
package com.mycompany.expense; ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access