I started learning Spring framework and before Spring I used Laravel. In Laravel we had various helpers for generating urls, for example:
<a href="{{ action('MyController@someAction') }}">Some url</a>
Is there anything similar in Thymeleaf, basically I want to generate an url which will point to some action in controller so if I change the mapping of the controller the url will change for all anchor tags.
There is a library present for spring boot framework. Which you need to add in your project in order to generate link dynamically. The gradle dependency of this library is given below.
compile 'org.springframework.boot:spring-boot-starter-hateoas:2.1.4.RELEASE'
I am assuming your build system is gradle but if you are using maven then please use below syntax.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-hateoas</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
After than you can generate link dynamically as below.
WebMvcLinkBuilder.linkTo(MyController.class).slash("someAction").withSelfRel().getHref();
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