Search code examples
javajspspring-mvcjstl

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


Solution

  • 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}"/>