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?
public class UriComponentsBuilder extends Object implements UriBuilder, Cloneable. Builder for UriComponents . Typical usage involves: Create a UriComponentsBuilder with one of the static factory methods (such as fromPath(String) or fromUri(URI) )
@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With