How to get the base URL?

I have this structure:

WebContent resources components top.xhtml company about_us.xhtml index.xhtml 

top.xhtml is a component that is also used in index.xthml and about_us.xhtml .

top.xhtml

 <ul> <li><a href="index.xhtml">Home</a></li> <li><a href="company/about_us.xhtml">About us</a></li> ... </ul> 

So, my problem is that when the current page is index.xhtml , the component generates URLs correctly, but when the current page is about_us.xhtml , it generates the wrong URLs. I cannot use the relative path because it will generate the wrong URL. I think this is because the component is based on the current *.xhtml page *.xhtml .

The only solution I could find was:

 <ul> <li><a href="${pageContext.request.contextPath}/webname/index.xhtml">Home</a></li> <li><a href="${pageContext.request.contextPath}/webname/about_us.xhtml">About us</a></li> ... </ul> 

But I think this is not "elegant." Any ideas?

+61
java jsf jsf-2
Jul 29 '11 at 19:49
source share
2 answers

URLs are not resolved based on server-side file structure. URLs are resolved based on the actual public web addresses of the respective resources. This is the web browser that has to link to them, and not the web server.

There are several ways to alleviate pain:

JSF EL offers the abbreviated expression ${pageContext.request} in flavor #{request} :

 <li><a href="#{request.contextPath}/index.xhtml">Home</a></li> <li><a href="#{request.contextPath}/about_us.xhtml">About us</a></li> 

You can use <c:set> if necessary to make it even shorter. Put it somewhere in the main template, it will be available for all pages:

 <c:set var="root" value="#{request.contextPath}/" /> ... <li><a href="#{root}index.xhtml">Home</a></li> <li><a href="#{root}about_us.xhtml">About us</a></li> 

JSF 2.x offers <h:link> , which can take a view identifier relative to the context root in the outcome , and it will automatically add a context display and FacesServlet :

 <li><h:link value="Home" outcome="index" /></li> <li><h:link value="About us" outcome="about_us" /></li> 

HTML offers a <base> tag that makes all the relative URLs in the document relative to this base. You can use it. Put it in <h:head> .

 <base href="#{request.requestURL.substring(0, request.requestURL.length() - request.requestURI.length())}#{request.contextPath}/" /> ... <li><a href="index.xhtml">Home</a></li> <li><a href="about_us.xhtml">About us</a></li> 

(note: this requires EL 2.2, otherwise you better use JSTL fn:substring() , see also this answer )

It should turn out in the generated HTML something like

 <base href="http://example.com/webname/" /> 

Please note that the <base> has a caveat: it does all the anchor bindings on the page, for example, <a href="#top"> with respect to it! See Also. Is it recommended to use the <base> html tag? In JSF, you can solve it as <a href="#{request.requestURI}#top">top</a> or <h:link value="top" fragment="top" /> .

+133
Jul 29 '11 at 20:00
source share

JSTL 1.2 change due to BalusC response

 <c:set var="baseURL" value="${pageContext.request.requestURL.substring(0, pageContext.request.requestURL.length() - pageContext.request.requestURI.length())}${pageContext.request.contextPath}/" /> <head> <base href="${baseURL}" /> 
0
Jun 17 '15 at 4:41
source share



All Articles