Following is the difference btwn Action and ActionListener in JSF
actionListener
Use
actionListener
if you want have a hook
before the real business action get executed, e.g. to log it, and/or to set an additional property (by
), and/or to have access to the component which invoked the action (which is available by
ActionEvent
argument). The
actionListener
method has by default the following signature:
import javax.faces.event.ActionEvent;
// ...
public void actionListener(ActionEvent event) {
// ...
}
Note that you can't pass
additional arguments by EL 2.2. You can however override the
ActionEvent
argument altogether by passing and specifying custom argument(s). The following examples are valid:
actionListener="#{bean.methodWithoutArguments()}"
actionListener="#{bean.methodWithOneArgument(arg1)}"
actionListener="#{bean.methodWithTwoArguments(arg1, arg2)}"
(note the importance of the parentheses in the argumentless method, if they were absent, JSF would still expect a method with ActionEvent
argument)
action
Use
action
if you want to execute a business action and if necessary handle navigation. The
action
method can return a
String
which will be used as navigation case outcome (the target view). A return value of
null
or
void
will let it return to the same page. A return value of empty string or
the same view ID will also return to the same page, but destroy any view
scoped beans. The
action
method can be any valid
MethodExpression
, also the ones which uses EL 2.2 arguments, e.g:
value="submit" action="#{bean.edit(item)}" />
with
public void edit(Item item) {
// ...
}
Note that when your action method solely returns a string, then you can also just specify exactly that string in the
action
attribute.
E.g. this is totally clumsy:
value="Go to next page" action="#{bean.goToNextpage}" />
with
public String goToNextpage() {
return "nextpage";
}
Instead just do
value="Go to next page" action="nexpage" />
Please note that this in turn indicates a bad design: navigating by
POST. This is not user nor SEO friendly. This all is explained in
When should I use h:outputLink instead of h:commandLink? and is supposed to be solved as
value="Go to next page" outcome="nextpage" />
Invocation order
The
actionListener
s are always invoked
before the
action
in the same order as they are been declared in the view and attached to the component. So, the following example
value="submit" actionListener="#{bean.listener1}" action="#{bean.submit}">
type="com.example.SomeActionListener" />
target="#{bean.property}" value="some" />
will invoke
Bean#listener1()
,
SomeActionListener#processAction()
,
Bean#setProperty()
and
Bean#submit()
in this order.
Exception handling
The
actionListener
supports a special exception:
AbortProcessingException
. If this exception is thrown from an
actionListener
method, then JSF will skip any remaining action listeners and the
action method and proceed to render response directly. You won't see an
error/exception page, JSF will however log it. If you intend to block
the page by an error page as result of a business exception, you should
be performing the job in the
action
method.
DISCLAIMER: I dont take any credit for this explanation. (because it was 'shamelessly' copied from BalusC's answer at Stack Overflow)