I have something like:
Locale defaultLocale = Locale.getDefault();
final Context ctx = new Context(defaultLocale);
String url = getHost() + "/page?someId=" + some.getId() + "&someParam=" + Boolean.TRUE;
ctx.setVariable("url", url);
final String htmlContent = templateEngine.process("theHtmlPage", ctx);
But when I look at the resulting HTML to print url
, it shows &
instead of &
in the URL.
Any suggestions?
I tried using backticks to escape the ampersand in the Java code, but it just printed those too. Looked around on SO, but didn't find much that was relevant. Also tried &
Update: Ok, this won't break the link, but Spring doesn't seem to resolve the parameter "someParam" as true without it.
Rendering tag:
<span th:utext="${url}"></span>
Output:
<span>http://localhost:8080/page?someId=1&someParam=true</span>
For example, to encode a URL with an ampersand character, use %24. However, in HTML, use either & or &, both of which would write out the ampersand in the HTML page.
It is the HTML entity for an ampersand ( & ) and is the proper character representation of it in a properly encoded URL. Ampersands ( & ) and well as < and > are special characters in XML and HTML and need to be displayed using their special character entities.
Thymeleaf template files are located in the custom src/main/resources/mytemplates directory. The default template directory is src/main/resources/templates . This is the Maven build file.
To avoid this kind of problems instead of '&' symbol you can use UTF code for that symbol, e.g in case of UTF-8 use '\u0026'.
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