Search code examples
jsfjsf-2facelets

JSF2: how achieve site-wide viewParam handling policy using a template


The following seems to be ignore inside XHTML facelet templates:

<f:metadata>
    <f:viewParam name="id" value="#{backingBean.id}" />
</f:metadata>

(At least with Mojarra as per Glassfish2.2)

Java Server Faces 2.0 Complete Reference says p.540:

The f:metadata tag encapsulates the set of elements used to specify the metadata for a Facelet view, and therefore must be a child of the f:view tag and may not appear in a template. As of JSF2.0, the only purpose of this tag is to encapsulate f:viewParam tags.

It is tedious for my current application to have to include the above in every one of hundreds of XHTML pages, but it fails in the template, I can't introduce view parameter handling policies. Indeed what I would like is both, a mixture of view parameter handling policies common to all pages and then some extra handling for specific pages that take additional query parameters.

Glad for advice,

Webel


Solution

  • Unfortunately, that's by spec. Your best bet is to use @ManagedProperty instead.

    public class BackingBean {
    
        @ManagedProperty("#{param.id}")
        private Long id;
    
        // ...
    }
    

    The only disadvantage is that this doesn't offer the advantage of using declarative conversion/validation by XHTML. You can if necessary do this job in a @PostConstruct method.