How to conditionally include a file in my template using JSF and Facelets?
Asked Answered
I

1

6

So, my template includes a footer.xhtml

<ui:include src="/WEB-INF/testtaker/Footer.xhtml"/>

What I want to do is change the footer based on some users pref to different Footer_???.xhtml file.

So, I'd like to do something like this:

<ui:include src="/WEB-INF/testtaker/Footer_001.xhtml">
      Content from original Footer.xhtml
</ui:include>

and if Footer_001.xhtml doesn't exist, then let it use the content between the tags, otherwise use the content from the file.

I know this seems a little odd, but this will solve a huge problem of customizing my existing site with out having to make changes to includes all over the place. Plus I'm not sure the file will exist before hand or not.

Any thoughts?

Inexertion answered 20/1, 2012 at 20:24 Comment(0)
C
4

You can use EL in <ui:include src>.

<ui:include src="/WEB-INF/testtaker/Footer#{user.prefs.footerId}.xhtml" />

If #{user.prefs.footerId} returns null or an empty string, it'll become just Footer.xhtml.

Chink answered 20/1, 2012 at 20:48 Comment(2)
Ah, yes. I'm planning on doing something like that. But the issue comes when the src resolves to something like Footer_001.xhtml and that file doesn't exist (yet) so I want to include the "default" Footer.xhtml's content. Does that make sense? :)Inexertion
Why would you have an invalid footer ID in the first place? Is this 100% user controlled input? (if so, are you aware about attack vectors in such case?) Why not just provide the enduser a dropdown with available footer IDs? Anyway, you could check in the prefs constructor or maybe the getter method if that footer exists. You could do that by checking if ExternalContext#getResource() returns null or not. If it doesn't exist, return null instead.Chink

© 2022 - 2024 — McMap. All rights reserved.