hc0re
hc0re

Reputation: 1986

How to create an URL in thymeleaf using parameter passed to the template?

I have created a html template for emails.

Now, i am putting a variable to the context:

context.setVariable("invoiceId", invoiceId);

In the template I have an tag:

<p><span>To accept the invoice, click <a th:href="http://localhost/accept/${invoiceId}">HERE</a></span></p>

but when I am running the app, I get:

org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "http://localhost/accept/${invoiceId}" (template: "mailTemplate" - line 7, col 47)

How can I use the ${invoiceId} variable in this case?

Upvotes: 0

Views: 1255

Answers (1)

tucuxi
tucuxi

Reputation: 17945

Normally, you communicate controllers and views using the model (I am assuming that you use Spring MVC, since you are already using Spring Boot). The only difference here would be

 model.addAttribute("invoiceId", invoiceId); 

Regardless of how you communicate the information, you should be using url templates when generating urls. Those start with @, and generally allow your application to be moved around without having to hard-code its address anywhere:

 <a th:href="@{/accept/{id}(id=${invoiceId})}">link text</a>

Note how thymeleaf handles those parameters: you use placeholders such as {foo} or {bar} within your url template, and then explain what they mean at the end, with something like (foo=${baz},bar=${quux}), where the contents of the expressions inside the ${} can be anything that thymeleaf can interpret.

Upvotes: 1

Related Questions