3

I use Icefaces and JSF and I have this problem: I have the following url: http://myMappedServer/myApp/followingThings

I would like to get in my xHtml page the value http://myMappedServer/myApp How can I achieve this without using the managed bean?

Himanshu
  • 31,810
  • 31
  • 111
  • 133
aika
  • 73
  • 1
  • 1
  • 6

1 Answers1

9

Use EL: #{request.contextPath}.

It's quite useful for creating navigation links, to set in your main template a Facelets variable :)

<ui:param name="root" value="#{request.contextPath}/" />

UPDATE: It's not recommended to use the full path available in the app server because it's not guaranteed to be the same URL the user is using to access your app, so, beware of that.

If you really want, though, you can do that, using some methods available in HttpServletRequest to create an String like this:

#{request.scheme}://#{request.serverName}:#{request.serverPort}#{request.contextPath}
Community
  • 1
  • 1
Elias Dorneles
  • 22,556
  • 11
  • 85
  • 107
  • @aika right, I've updated the answer with how to do that. Note, though, that it's not recommended to use it for creating links because it's not guaranteed to be the same address the user is using to access it (your app may be behind a proxy, or a load balancer, for example). – Elias Dorneles Jul 27 '12 at 14:49
  • hello eljunior, indeed I want to get the domain mapped by the proxy. Is there any way to do it? thank you :) – aika Jul 27 '12 at 14:56
  • I am afraid not, not through the Servlet API/JSF at least. Note that using only the contextPath will work fine for navigation links in the app, you only would need to use the full url for things off the app like an email or report. In this case, you're probably better making the URL a config in a properties file and expose it in an `@ApplicationScoped` bean. If you REALLY don't want to do that for some reason, maybe you'd have more luck asking a question specific to your proxy, like "how to get the user url behind nginx?" or something. – Elias Dorneles Jul 27 '12 at 16:14
  • I was really looking for this answer thanks so much. – Guilherme Jan 08 '17 at 14:12