Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the full url, including hostname with jstl

<c:url var="myUrl" value="/MyPath/${MyID}"/>

which I then use later (to enable users to copy links) :

<input size="35" disabled value="${myUrl}" />

and it shows

/my-app-name/MyPath/23

however I want it to be

http://myHost/my-app-name/MyPath/23

I can prepend the string sure, but wanted a way to actively get the correct hostname ... ?

like image 686
NimChimpsky Avatar asked Oct 06 '11 13:10

NimChimpsky


2 Answers

You need to prepare it yourself based on HttpServletRequest#getRequestURL() and a little help of JSTL functions:

<c:set var="req" value="${pageContext.request}" />
<c:set var="baseURL" value="${fn:replace(req.requestURL, fn:substring(req.requestURI, 1, fn:length(req.requestURI)), req.contextPath)}" />
...
<c:url var="myUrl" value="${baseURL}/${MyID}"/>
like image 92
BalusC Avatar answered Nov 17 '22 21:11

BalusC


HttpServletRequest object has all the details:

  • getProtocol
  • getServerName
  • getContextPath

so I think you can use:

${request.protocol} :// ${request.serverName} ${request.contextPath} /etc

to build what you want.

like image 30
helios Avatar answered Nov 17 '22 22:11

helios