Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Thymeleaf: Generate URL for action in controller

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.

like image 660
Alen Avatar asked Jun 19 '26 13:06

Alen


1 Answers

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();
like image 156
Tushar Saxena Avatar answered Jun 21 '26 16:06

Tushar Saxena



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!