Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a URL that includes an ampersand with Thymeleaf?

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 &amp 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&amp;someParam=true</span>
like image 396
vphilipnyc Avatar asked Apr 06 '15 01:04

vphilipnyc


People also ask

Can you put ampersand in URL?

For example, to encode a URL with an ampersand character, use %24. However, in HTML, use either &amp; or &#38;, both of which would write out the ampersand in the HTML page.

What is &amp in a URL?

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.

Where do I put Thymeleaf templates?

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.


1 Answers

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'.

like image 55
Meruzhan Kerobyan Avatar answered Sep 23 '22 11:09

Meruzhan Kerobyan