6

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>
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
marioosh
  • 24,528
  • 42
  • 132
  • 181
  • What about navigation rules ? – boblemar Nov 02 '11 at 10:15
  • No navigation rules. I need only to find a simple way in JSF 2.0 to make links generated with context-path automatically. In normal way i use ``, but it is requirement that no use JSTL. – marioosh Nov 02 '11 at 11:26

1 Answers1

11

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • What if I don't have outcome or navigation rule, for example: ``. This give an error: `Unable to find matching navigation case from view ID '/pages/home.xhtml' for outcome 'res/file.pdf'` – marioosh Nov 07 '11 at 12:28
  • 3
    Then use `Download` or just `Download` or use HTML ``. – BalusC Nov 07 '11 at 12:38