Sunday, 25 October 2009

Actions not being called when using 2 view tags

I had a problem where a command button's action was not being called on the managed bean.

When using tiles inside JSF, you may have it set up so that you have a template.jsp and a body.jsp.
The template.jsp, using tiles inserts body.jsp.

template.jsp defines a <f:view> tag as per normal.
Inside body.jsp, whilst you don't technically need a <f:view> tag, you may get some component (namely f:phaseListener) complaining about not having a view root, so you does it asks and defines the <f:view> at the start of the file.

You load up the page, everything renders correctly and you click a submit button. The form validation errors appear as normal, so you fill in some values and click submit again.
This time, NOTHING.
So you try turning on all the logging and still nothing seems wrong.

This happened to me. I wasted quiet a few hours on this one: working out how to log things correctly using eclipse/tomcat and get Java Util Logging working etc. After a ton of breakpoints, it appeared that there were no 'events' cued for the view root to invoke in the Invoke Application Phase. Some more breakpoints showed that the button was being decoded and an event was being added to the queue. However it wasn't in the main view root. Strange.

After some pondering I decided that the problem must be that the included page wasn't sending the events up.

So the solution:
When using tiles, inside the included page use <f:subview id="'body'"> not <f:view>.

However, this means that you can't use a <f:phaseListener> tag in the included page.

I was using mojarra-1.2_12.