[英]Escape single/double quotes from Stripes action bean jquery
我有以下JavaScript函數,
function check() {
document.getElementById("customer.name").value = "${actionBean.customer.name}";
.....
}
值$ {actionBean.customer.name}可能帶有'或“”引號。 如何從javascript方法中擺脫出來?
例如,actionBean.customer.name動態變為"HI "I'M HOME""
干杯!!
您可以嘗試這樣做:
function check() {
var customerName = (${actionBean.customer.name}).replace(/\"/g, '"').replace(/\'/g, ''');
document.getElementById("customer.name").value = customerName;
// continue function
}
org.apache.commons.lang3.StringEscapeUtils
可以為您做到這一點。 它具有方法escapeEcmaScript(String input)
。
org.apache.commons.lang.StringEscapeUtils
中的舊版本包含類似的方法escapeJavaScript(String input)
。
我通常創建一個StringFunctions類,該類由靜態函數組成,例如包裝了escapeEcmaScript函數:
public static String escapeEcmaScript(String s) {
return StringEscapeUtils.escapeEcmaScript(s);
}
在標記庫描述符中包含StringFunctions
類:
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" version="2.0">
<tlib-version>1.0</tlib-version>
<short-name>tlb</short-name>
<uri>http://www.trilobiet.nl/taglib/trlbt</uri>
<function>
<name>escapeEcmaScript</name>
<function-class>
com.trilobiet.apollo.stripes.viewhelpers.StringFunctions
</function-class>
<function-signature>
String escapeEcmaScript(java.lang.String)
</function-signature>
</function>
<!-- more functions -->
</taglib>
在jsp中包含標簽庫描述符:
<%@ taglib prefix="tlb" uri="/WEB-INF/taglib/tlb.tld" %>
(當然,您可以隨意使用對您而言合理的任何短名稱和taglib前綴。)
像這樣使用:
${tlb:escapeEcmaScript(actionBean.customer.name)}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.