This question is not about URI-encoding links in Thymeleaf, but about URL-encoding links, which is a different thing.
I'm using Thymeleaf with Spring MVC in Spring Boot. I know that I can URI-encode a link in Thymeleaf using @{…} syntax, as explained in Thymeleaf: Unencode or Encode URL on client. However I would like to URL-encode links.
I have an HTML form that sends a query in the form foo+bar using the application/x-www-form-urlencoded media type, which I am referring to as "URL encoding" However Thymeleaf's @{…} syntax encodes link parameters using the URI encoding foo%20bar.
Normally the newer URI encoding would be preferred, but I have another page that uses old-school HTML forms, which invariably send form data using URL encoding.
Fortunately (in this case) Spring MVC seems to accept both the foo+bar and the foo%bar encodings, but I think being wishy-washy on the format is bad form (pun unintended). I'd like to make the site uniform, which will also help users in creating consistent bookmarks. (Maybe in the future I will decide to only accept one format for the links.)
How can I tell Thymeleaf to URL-encode links using the foo+bar form? I'd prefer not to use manual string replacement, but I could accept that as a last resort. (Following another Stack Overflow answer, I tried using @{…#strings.replace(data,' ','+')}, but the @{…} wound up URI-encoding the +, producing foo%2Bbar.)
Alternatively, is there a simple way to tell my HTML form to submit data using the foo%20bar form? (I tried using the Thymeleaf @{…} syntax in the form data, to preformat it before submission, but couldn't get it to work on non-link data; this left the spaces untouched, leaving them for the <form> to URL-encode them using +.)
If you want to create URLs with the "foo+bar" style encoding for consistency with your older HTML forms, while Thymeleaf's @{...} syntax uses the newer "foo%20bar" style encoding.Thymeleaf's #strings utility object to perform the encoding in the desired format.
replace someUrl with your actual URL and uriEncodingParam with the URI-encoded parameter.