Is a better way to generate absolute links in JSF 2.0 ? Right now I'm using <h:outputLink/>
in that ugly way with #{facesContext.externalContext.requestContextPath}
like below. I don't want to use JSTL and <c:url />
<h:outputLink value="#{facesContext.externalContext.requestContextPath}/pages/home.jsf">Home</h:outputLink>
A relative URL is useful within a site to transfer a user from point to point within the same domain. Absolute links are good when you want to send the user to a page that is outside of your server.
If you prefix the URL with // it will be treated as an absolute one. For example: <a href="//google.com">Google</a> . Keep in mind this will use the same protocol the page is being served with (e.g. if your page's URL is https://path/to/page the resulting URL will be https://google.com ).
Using relative paths allows you to construct your site offline and fully test it before uploading it. An absolute path refers to a file on the Internet using its full URL. Absolute paths tell the browser precisely where to go. Absolute paths are easier to use and understand.
Unlike absolute paths, relative paths contain information that is only relative to the current document within the same website which avoids the need to provide a full absolute path. In simple words, relative path refers to a path relative to the location of the current webpage.
You can shorten #{facesContext.externalContext.requestContextPath}
to #{request.contextPath}
. You can even get rid of it to use HTML <base>
tag instead.
In this particular case, better is to use <h:link>
instead. It can take a context-relative navigation case path in outcome
attribute:
<h:link value="Home" outcome="pages/home" />
JSF will take care about adding the right context path and FacesServlet
mapping while generating the <a>
element:
<a href="/contextname/pages/home.jsf">Home</a>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With