从 Stripes 操作 bean jquery 中转义 single/double 引号
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 class,由静态函数组成,例如包装 escapeEcmaScript 函数:
public static String escapeEcmaScript(String s) {
return StringEscapeUtils.escapeEcmaScript(s);
}
在标记库描述符中包含 StringFunctions
class:
<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" %>
(当然,您可以自由使用任何听起来合乎逻辑的短名称和标签库前缀。)
然后像这样使用:
${tlb:escapeEcmaScript(actionBean.customer.name)}
我有一个 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 class,由静态函数组成,例如包装 escapeEcmaScript 函数:
public static String escapeEcmaScript(String s) {
return StringEscapeUtils.escapeEcmaScript(s);
}
在标记库描述符中包含 StringFunctions
class:
<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" %>
(当然,您可以自由使用任何听起来合乎逻辑的短名称和标签库前缀。)
然后像这样使用:
${tlb:escapeEcmaScript(actionBean.customer.name)}