Search code examples
javajsfjakarta-eejsf-2java-ee-6

Where to put composition components?(JSF 2.0)


I am continuing my practices with JSF 2.0. I see templating is a great thing to do, and it has lots of advantages. But today i got a new doubt related to it.

I created a template for my pages. In the template, i use tags for the parts that are different(Those parts will be implemented later in a page using the composition tag in combination one or more define tags).

<ui:insert name="content" />

Also inside the template, to avoid putting to much code in the template, i create tags to add some other chunks of xhtml.

<ui:include src="/languageChanger.xhtml"/>

This is how my folder structure looks:

enter image description here

It all works as i spect, but when in the url i navigate to languageChanger.xhtml i see the composite chunk of xhtml:

enter image description here

My doubts are:

-Is that chunk of independent code placed in the right place?, Or it is wrong, the user should not be allowed to see that from the URL?

-Is that place save to have other components like login, register...?

-To avoid user access directly the component i could place it in WEB-INF folder, but then i have a problem that the include tag does not find the path. What should i do?

-What would be the best practice, where to place this independent chunks of code?


Solution

  • Is that chunk of independent code placed in the right place?, Or it is wrong, the user should not be allowed to see that from the URL?

    Put it somewhere in /WEB-INF. Direct access to this folder is disallowed by the container.


    Is that place save to have other components like login, register...?

    I don't understand you. Perhaps you meant to say "safe" instead of "save"? What do you mean with "other components"?


    To avoid user access directly the component i could place it in WEB-INF folder, but then i have a problem that the include tag does not find the path. What should i do?

    Your path was apparently plain wrong. Facelet templates, includes, tags and compositions (not composite components) can perfectly be placed in /WEB-INF.


    What would be the best practice, where to place this independent chunks of code?

    Put it in /WEB-INF. Best practice is to use absolute paths, i.e. start the path with /. It will be resolved relative to the webcontent root. E.g.

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

    Only the "main" page (the one which is to be requested by URL) cannot be placed in /WEB-INF.