Monday, December 31, 2012

JSF 2.x Tip of the Day: Dynamic <ui:include/> with <f:viewParam/>

Introduction

I encountered a situation the other day where I need to include a JSF page based on a parameter passed in via a <f:viewParam>. This seems like a very reasonable kind of idea. I expected to be able to make the <ui:include/> use a dynamically set src parameter using EL from the <f:viewParam>. This is where I went wrong (the idea was good...). The <ui:include/> is a tag handler. It is evaluated on Restore View Phase. The value processing does not take place until the Apply Request Values Phase. This presents a problem since the EL binding for my <ui:include/> is based on the <f:viewParam> which is a UIInput object. So we are out of phase. The page will display the results, but all the AJAX will not work.

A Solution

A solution to the problem involves using static <ui:include/> elements wrapped in <ui:fragment/> elements. This allows the tag handler to resolve the page to include, and then the fragment determines (via late binding) whether to display the page based on the <f:viewParam> passed. The page is supported by a @ViewScoped bean to keep the page in scope.

Code

Here is an example of how to do it.

Conclusion

I have put the complete NetBeans 7.2.1 project using Mojarra on Bitbucket. The project uses Apache Maven, and was tested using GlassFish 3.1.2. Here is the complete project: dynamic-include

0 comments :

Popular Posts