Istvan Szasz
Istvan Szasz

Reputation: 1567

Create a link to a JSF page in a subfolder

I have a Java web app with the following structure:

- Project
     - Web Pages
          + WEB-INF
          + resources
          - administration
               systemsettings.xhtml
               userhandling.xhtml
          login.xhtml
          index.xhtml
          menubar.xhtml
          ...
     + Source Packages ...

I recently added the administration folder to the Web Pages, and I moved there the two .xhtml pages so I can write more specific URL pattern for my administrator authorization filter.

However, I don't know what is the path for the pages in the administration folder. While <a href='index.xhtml'> works, <a href='administration/systemsettings.xhtml'> doesn't, throws java.io.FileNotFoundException.

Update:

The content of the systemsettings.xhtml was:

<h:body>
    <ui:insert name="header" >
        <ui:include src="menubar.xhtml" />
    </ui:insert>
    <div>Administration - System settings</div>
</h:body>

I forgot to update the path to the included page, after the refactor. This can be fixed, as @BalusC suggested, by changing the <ui:include src="menubar.xhtml" /> to <ui:include src="/menubar.xhtml" />.

Upvotes: 2

Views: 3418

Answers (1)

BalusC
BalusC

Reputation: 1108742

If a link does not start with a scheme like http://, then it's a relative link which is relative to the current request URI as you see in browser's address bar. If a relative link starts with /, then it's relative to the domain root in request URI. If a relative link does not start with / then it's relative to the currently opened folder in request URI.

If you can't tell about the currently opened folder for sure, then you need to make it a domain-relative link by explicitly prepending the context path as available by #{request.contextPath}.

<a href="#{request.contextPath}/administration/systemsettings.xhtml">settings</a>

An alternative is to just use JSF <h:link> component. It will automatically prepend the context path (and also automatically apply the JSF mapping!). The following <h:link> examples generate equivalent HTML.

<h:link value="settings" outcome="/administration/systemsettings.xhtml" />
<h:link value="settings" outcome="/administration/systemsettings" />

Upvotes: 2

Related Questions