Lucky
Lucky

Reputation: 17345

How can I get request parameters from REST style URL in thymeleaf?

In the thymeleaf documentation there's only option to get request parameters from urls which looks like,

https://example.com/getUser?id=10

I can use ${param.id[0]} to access the user id in thymeleaf whereas if I have a REST-style url like this,

https://example.com/user/10

How can I access the user id(10) in the page using thymeleaf with the above URL? Of course I can set a model attribute to access the id of user in the page. But just wondering if there any better way to do this in thymeleaf in order to minimize the code?

Upvotes: 2

Views: 312

Answers (1)

destan
destan

Reputation: 4411

Well there is no direct helper AFAIK but your best shot is something like this:

${#strings.listSplit(#httpServletRequest.requestURI,'/')[1]}

note that 1 which is the list index of splitted url varies according to your url structure.

Note that #httpServletRequest.requestURI gives you user/10 and listSplit gives you [user, 10]

Upvotes: 2

Related Questions