13

I use spring 3.0 and I have a really simple question but didn't find any answer on the internet. I want to generate a path (an URI) just like in my JSPs:

<spring:url value="/my/url" />

But inside a controller. What is the related service to use? Thanks!

Edit: May it be related with this: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources.html#resources-resourceloader ? Ain't there a better solution for this?

Nanocom
  • 3,696
  • 4
  • 31
  • 46

3 Answers3

36

Rossen's suggestion is gold.

There's also the ServletUriComponentsBuilder class from 3.1 that builds URLs from current request in static fashion. For example:

ServletUriComponentsBuilder.fromCurrentContextPath().path("/my/additional/path").build().toUriString();

It's the closest thing to <spring:url> in servlet.

Christopher Yang
  • 3,769
  • 4
  • 30
  • 27
20

In Spring MVC 3.1 you can use the UriComponentsBuilder and its ServletUriComponentsBuilder sub-class. There is an example of that here. You can also read about UriComponentsBuilder in the reference docs.

Rossen Stoyanchev
  • 4,910
  • 23
  • 26
  • Thank you, but I forgot to tell that I use Spring 3.0... Saw it's new in psring 3.1, so I assume there is no such URI builder in Spring 3.0 – Nanocom Jan 09 '12 at 21:22
2

I would say

request.getRequestURL() + "/my/url"

makes the job. There is no such built in functionality, spring:url calls UrlTag.class that has the below method to generate URL, you can use it as an insőiration for your 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 {
            url.append(this.context);
        }
    }
    if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !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 = isHtmlEscape() ? HtmlUtils.htmlEscape(urlStr) : urlStr;
    urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

    return urlStr;
}
Peter Szanto
  • 7,568
  • 2
  • 51
  • 53
  • That's the bad way to do this. I need the service that is used by to generate urls... Need the clean way, please – Nanocom Jan 06 '12 at 19:14
  • Bad in what way? There is no such service, spring:url more or less does the same, see the source attached. – Peter Szanto Jan 07 '12 at 11:00
  • Ok, if you say it... But that's crazy that they didn't think about that, in my opinion! – Nanocom Jan 07 '12 at 12:36
  • hmm, true. Probably they expect this to be handled only in the view tier. – Peter Szanto Jan 07 '12 at 17:07
  • Yes maybe, but I needed it to generate an automatic message containing a link and to save it in the database, it's quite a normal need according to me – Nanocom Jan 07 '12 at 20:16