I'm trying to escape the apostrophes in the value of a variable that is passed as a string parameter using thymeleaf. My code looks like this:
<button type="button" class="btn btn-default btn-sm" th:id="${'delete'+document['oid_pj']}"
th:if="${document['utilisateur'] == session.utilisateur}"
th:onclick="'deletePj(\''+${document['oid_pj']}+'\',\''+ ${document['url']} +'\');'">
and when the html page is interpreted it looks like this:
<button type="button" class="btn btn-default btn-sm" id="deleteAS8bo00000F1T" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d'un service GtMonitor.doc');">
so the apostrophe from "d'un" closes the parameter before it's meant to.
Does anyone know how can i escape it?
您可以尝试切换使用引号的方式
onclick='"deletePj(\""+${document["oid_pj"]}+"\",\""+ ${document["url"]} +"\");"'>
Before passing ${document['url']}
(the value containing single quote) to the javascript function, clean it first by replacing '
with \\'
. Use th:with
to create a local variable which will be used to store the cleaned value. Use #strings.replace
to do the replacement. To prevent compilation errors, use html entity '
instead of '
when replacing
<input type="button" value="Click Me"
th:with='cleaned=${#strings.replace(document["url"], "'", "\'")}'
th:onclick="'deletePj(\''+${document['oid_pj']}+'\',\''+ ${cleaned} +'\');'"/>
Output :
<input type="button" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d\'un service GtMonitor.doc');" value="Reconciliation">
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.