在 thymeleaf 变量中转义撇号
escape apostrophe in thymeleaf variable
我正在尝试转义使用 thymeleaf 作为字符串参数传递的变量值中的撇号。我的代码如下所示:
<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']} +'\');'">
当 html 页面被解释时,它看起来像这样:
<button type="button" class="btn btn-default btn-sm" id="deleteAS8bo00000F1T" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d'un service GtMonitor.doc');">
所以来自 "d'un" 的撇号在它的意思之前关闭了参数。
有谁知道我怎样才能逃脱它?
您可以尝试切换使用引号的方式
onclick='"deletePj(\""+${document["oid_pj"]}+"\",\""+ ${document["url"]} +"\");"'>
在将 ${document['url']}
(包含单引号的值)传递给 javascript 函数之前,首先通过将 '
替换为 \'
来清理它。使用 th:with
创建一个局部变量,用于存储清理后的值。使用 #strings.replace
进行替换。为防止编译错误,在替换
时使用html实体'
而不是'
<input type="button" value="Click Me"
th:with='cleaned=${#strings.replace(document["url"], "'", "\'")}'
th:onclick="'deletePj(\''+${document['oid_pj']}+'\',\''+ ${cleaned} +'\');'"/>
输出:
<input type="button" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d\'un service GtMonitor.doc');" value="Reconciliation">
我正在尝试转义使用 thymeleaf 作为字符串参数传递的变量值中的撇号。我的代码如下所示:
<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']} +'\');'">
当 html 页面被解释时,它看起来像这样:
<button type="button" class="btn btn-default btn-sm" id="deleteAS8bo00000F1T" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d'un service GtMonitor.doc');">
所以来自 "d'un" 的撇号在它的意思之前关闭了参数。
有谁知道我怎样才能逃脱它?
您可以尝试切换使用引号的方式
onclick='"deletePj(\""+${document["oid_pj"]}+"\",\""+ ${document["url"]} +"\");"'>
在将 ${document['url']}
(包含单引号的值)传递给 javascript 函数之前,首先通过将 '
替换为 \'
来清理它。使用 th:with
创建一个局部变量,用于存储清理后的值。使用 #strings.replace
进行替换。为防止编译错误,在替换
'
而不是'
<input type="button" value="Click Me"
th:with='cleaned=${#strings.replace(document["url"], "'", "\'")}'
th:onclick="'deletePj(\''+${document['oid_pj']}+'\',\''+ ${cleaned} +'\');'"/>
输出:
<input type="button" onclick="deletePj('AS8bo00000F1T','//20150007/GIWkE0000FHUI//Creation d\'un service GtMonitor.doc');" value="Reconciliation">