Java Reference
In-Depth Information
14.3.1
BeanBase
It's important to understand the purpose of the
BaseBean
before we look into the
ActionContext
and the
BeanAction
. The
BaseBean
extends
ValidatorActionForm
to allow for standard Struts validation to occur. Instead of extending an
Action-
Form
directly, you extend the
BaseBean
. The
BaseBean
contains your normal prop-
erties, as an
ActionForm
normally would. Thus, it is populated as Struts would
populate the
ActionForm
because it
is
an
ActionForm
. The only difference is that
your extended
BaseBean
would also contain behavior methods that follow the sim-
plified signature of
public
String
methodName()
.
14.3.2
BeanAction
The next piece of this puzzle that we should introduce is the
BeanAction
. The
BeanAction
has a couple of responsibilities. First, the
BeanAction
populates the
ActionContext
. Next, it routes behavior calls to your extended
BaseBean
and trans-
lates the returned behavior method's String into an
ActionForward
for Struts. This
is how the
BaseBean
is able to stay clear of Struts-specific components in the
behavior signatures. The
BeanAction
class looks in two different places to deter-
mine which behavior method to call on the extended
BaseBean
. First, it checks to
see if the action mapping has a specified parameter that explicitly states the
method to call. If the parameter specifies
*
, then a method is not called and the
success action forward is used. If the action mapping
parameter
attribute is not
specified or is empty, then the
ActionBean
looks at the path and uses the filename
minus the extension for the method call. So, if you used a standard
.do
mapping
and had a path that ended with
/myMethod.do
, the
myMethod
behavior method
would be called on your extended
BaseBean
.
14.3.3
ActionContext
Finally, the
ActionContext
is used to abstract from specific web semantics. It pro-
vides you access to the request, parameters, cookies, session, and application all
through a
Map
interface. This gives you the option of reducing direct dependen-
cies on the web layer. Most of the
ActionContext
successfully isolates you from
Struts and even the Servlet
API
. However, the
ActionContext
still provides direct
access to the
HttpServletRequest
and the
HttpServletResponse
for those times
when you need access to it.
There are several advantages to this approach. First of all, we don't need to spend
time and code casting
ActionForm
objects to their extended types. This is avoided
because the
Action
is the
ActionForm
. All you need to do is access the properties
directly on the bean object that extends
BaseBean
. Second, the behavior methods
