Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ActionListeners not working when valdation is used.

 
Christopher Sharp
Ranch Hand
Posts: 157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the action isn't fired if the validation fails. Could that be the problem?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic