Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something better to generate absolute links?

Tags:

jsf-2

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>
like image 967
marioosh Avatar asked Nov 02 '11 10:11

marioosh


People also ask

Should I use relative or absolute links?

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.

How do you make an absolute URL?

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 ).

Is absolute path or relative path better?

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.

Why is relative path better than absolute?

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.


1 Answers

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>

See also:

  • Communication in JSF 2.0 - Implicit navigation
  • How get the base URL?
like image 56
BalusC Avatar answered Dec 31 '22 20:12

BalusC