Shale supports a mechanism that provides a 1:1 relationship between a view tier presentation technology, which is responsible for creating an HTTP response (such as a JSP page), and a corresponding JavaBean class containing event handling logic, (optionally) values used in the dynamic rendering of the response, and (optionally) bindings to the individual user interface components included in the response page. Such a JavaBean class is known (in JavaServer Faces terminology) as a backing bean. In most circumstances, such a bean will be registered as a managed bean (configured for creation in request scope).
JavaServer Faces does not require that a backing bean implement any
particular interface, or extend any particular base class. Therefore,
Shale does not impose any such restriction either. Instead, it promises
that if an application's backing bean implements the
ViewController
interface, then certain extra services will
be provided "for free."
Each backing bean that implements ViewController
will
support a boolean property postback
, which will be set to
true
if this view is the one that is processing a form
submit previously rendered by the same page, or false
if
this view was newly navigated to. The property will be set before any
of the lifecycle methods described below are called, so that application
logic may perform conditional tasks based on this state.
As part of the standard JavaServer Faces managed beans processing, any
<managed-property>
elements in the configuration
file, that are nested inside the <managed-bean>
element for this backing bean, will also be processed when a new bean
instance is created. You can use either literal values or value binding
expressions to customize properties on your backing bean class. Fans
of Dependency Injection will see that the managed beans facility
provides support for such a framework, using Setter Injection as the
mechanism for injecting dependencies.
In addition, the following lifecycle events are called, by the framework, at certain points in the JavaServer Faces request processing lifecycle:
ViewManager.createView()
is called.)
For a postback, this happens during the Restore View
phase of the request processing lifecycle, once it has been
determined which view should be restored. If your application
navigates from one page to another, the init()
method
of the second page will be called as part of the
NavigationHandler.handleNavigation()
processing.
Use this method to acquire resources that you will need, no matter whether this is a postback request, a rendering request, or both.
Use this method to acquire resources (such as database connections) that you will need to process the postback.
Use this method to acquire resources (such as database connections, or performing queries) that you will need if this view is the one to be rendered. NOTE - when portlet support is integrated, all of the portlets on the current page will receive this event.
init()
was ever called
for a ViewController
, then it is guaranteed that
destroy()
will be called as well.
Use this method to release any resources acquired during an earlier event handler.
setPostback(false)
and init()
methods
on your backing bean are called.prerender()
method is called.destroy()
method is called.null
from the action handler.):
setPostback(true)
and init()
methods
on your backing bean are called.prerender()
method is called.destroy()
method is called.setPostback(true)
and init()
methods
on your backing bean for page A are called.prerender()
method for page B is called.destroy()
method is called for both page B
and page A (since two backing beans were instantiated).ViewController
backing bean, you must:
ViewController
interface. The
most convenient way to do this is likely to be extending the
convenience base class (org.apache.shale.view.AbstractViewController
).<managed-bean>
,
using a <managed-bean-name>
value that can be
mapped from the view identifier. (See
DefaultViewControllerMapper
for the details of
the default mapping.)
In nearly all circumstances, you will want the bean to be placed
in request scope.