How to resolve the absolute path with <spring:url /> for relative URI
<p>I have a website index ..... <code>http://localhost/myContext/</code><br>
a REST index .................. <code>http://localhost/myContext/restController/</code><br>
and many REST pages ... <code>http://localhost/myContext/restController/*/*</code></p>

<p>The REST controller uses relative paths, loading the contents dynamically depending on the URL.</p>

<p>When these pages are hit with a direct browser access, all the pages works fine and you can navigate freely.</p>

<hr>

<p>Now I want to integrate the <code>restController/*/*</code> pages directly into the <code>/myContext/</code> index using AJAX. The contents are loaded into a DIV; <em>the browser's URL will never change</em>.</p>

<p>My browser hits the website index <code>/myContext/</code><br>
Then AJAX loads <code>/restController/</code>, which points to a JSP...</p>

<hr>

<p>Inside the JSP: <code>&lt;spring:url value="something"&gt;</code> returns directly <code>something</code> instead of the whole absolute path based on the controller's servletPath <code>/myContext/restController/something</code></p>

<p>Since <code>&lt;spring:url&gt;</code> returns a relative path, the browser tries to hit <code>/myContext/something</code> - <em>404</em></p>

<p>I did this previously for forms using <code>&lt;form:form&gt;</code> and it works great. The action is created relatively to the controller's servletPath.</p>

<p><strong>What is the most clean way to get an absolute path with <code>&lt;spring:url&gt;</code> (or other) from a relative URI?</strong></p>

<hr>

<p>I tried using <code>${pageContext.request.requestURI}</code> but it includes the JSP filename (<code>/myContext/restController/something/ctrl.jsp</code>), filtering the filename won't work since I rely on the presence or not of the last dash to navigate.</p>

<p>I also tried including a <code>&lt;form:form /&gt;</code> and recover the action using Javascript. It's very ugly but it works... Not a good solution tho.</p>

<hr>

<p>The code from <code>&lt;spring:url&gt;</code> :</p>

<pre><code>private String createUrl() throws JspException {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
        StringBuilder url = new StringBuilder();
        if (this.type == UrlType.CONTEXT_RELATIVE) {
            // add application context to url
            if (this.context == null) {
                url.append(request.getContextPath());
            }
            else {
                if (this.context.endsWith("/")) {
                    url.append(this.context.substring(0, this.context.length() - 1));
                }
                else {
                    url.append(this.context);
                }
            }
        }
        if (this.type != UrlType.RELATIVE &amp;&amp; this.type != UrlType.ABSOLUTE &amp;&amp; !this.value.startsWith("/")) {
            url.append("/");
        }
        url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
        url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

        String urlStr = url.toString();
        if (this.type != UrlType.ABSOLUTE) {
            // Add the session identifier if needed
            // (Do not embed the session identifier in a remote link!)
            urlStr = response.encodeURL(urlStr);
        }

        // HTML and/or JavaScript escape, if demanded.
        urlStr = htmlEscape(urlStr);
        urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

        return urlStr;
    }
</code></pre>