0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I solved it partially, but the problem is - I am generating an email template, and when I am using: <a href="http://localhost/" th:href="@{|accept/${invoiceId}|}">HERE</a> i get only accept/5d00d36b2f8c3230d23d388a as the link, http://localhost is nowhere to be found :/
Follow @tucuxi 's answer. <a th:href="@{/accept/{id}(id=${invoiceId})}">link text</a>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.