Action vs ActionListener
Hi there,
What exactly is the difference between an action and an actionListener for the commandButton? I know that an action must return a string and take no parameters, whereas the actionListener method should take an ActionEvent as a parameter and return void but can someone mention pratical examples when one should be used instead of the other?
cheers
c
[384 byte] By [
KrisMTa] at [2007-11-27 3:06:11]

# 1
> I know that an action must return a string
This is not true, an action method can also be declared as void.
The actionListener has just the advantage of the ActionEvent. You can for example retrieve the parent UIComponent element from it, which can be useful when you have multiple buttons which should invoke the same method and it's behaviour just differs per button. You can also retrieve the current PhaseId from it, which can be useful to track the current Phase.
Personally I find actionListener only useful due to the possibility to pass an parameter using f:attribute. Also see http://balusc.xs4all.nl/srv/dev-jep-com.html and the "Sorting datatables" part of http://balusc.xs4all.nl/srv/dev-jep-dat.html
# 2
Just because you can do it, doesn't mean you should do it. =)
IMHO, action methods should always return String because it is directly related to page navigation. And I don't think forcing navigation from an ActionListener is good practice.
I too rarely find ActionListener useful. You would use this if you need to access your component tree (because the event object gives you straight-forward access) for some reason other than navigation. Navigation related processing should go into your action methods.
CowKing
# 3
> IMHO, action methods should always return String
> because it is directly related to page navigation.
> And I don't think forcing navigation from an
> ActionListener is good practice.
IMHO, page-to-page navigation using POST is not very gently and all POST calls should go back to the same page. If it was GET, then I'd used navigation cases though.