File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes ActionListeners not working when valdation is used. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "ActionListeners not working when valdation is used." Watch "ActionListeners not working when valdation is used." New topic

ActionListeners not working when valdation is used.

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 155
I discovered to my horror that action listenders do not work when I use validation. This is really annoying, as otherwise the code works.

In an included xhtml file I have the following code, and have just started to use PrimeFaces:

In the main xhtml page that includes the above I have added some code for validation:

and in my bean there are a number of action methods, one of which is here.

On selecting a menu item then clicking the command button, the action abunSet0 is fired and data is read in from a file and a list box is populated. This now works much better with PrimeFaces than with straight jsf.

However, if I add a validation component in the main xhtml page, or indeed also in code in the page in the first listing, the action is not fired and the list box is not populated. Why is this and is there an easy way round this? I suspect it has something to do with the order the JSF cycle is executed, and somehow the validation prevents the action from being fired.

Many thanks for any help.
Greg Charles

Joined: Oct 01, 2001
Posts: 2967

Well, the action isn't fired if the validation fails. Could that be the problem?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

I strongly discourage the use of actionListeners. For 95 percent of what you do, an action method is just as capable, and unlike action listeners, can be coded with non-specific POJO code, which makes it easier to test offline and to re-use on other (non-JSF) platforms. I think the whole idea of frivolous use of actionListeners may be a leftover from the days when JSF was still being designed and the mechanisms were much cruder.

Speaking of mechanisms, though, it's a fundamental aspect of the JSF lifecycle that if ANY input value in a post is invalid, the entire operation is rejected. Not only are both action methods and action listeners not invoked, but the backing bean will not be update. The reason for this is that updates of the backing bean are expected to be all-or-none; no bits and pieces of dirty data. And the action code is guaranteed that the bean properties that it works with are not only valid, but have been updated to match the submitted form data before the action code was invoked.

So it's not a bug, it's not even a feature. It's a basic architectural concept.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: ActionListeners not working when valdation is used.
It's not a secret anymore!