A custom EL
function to escape javascript special characters present in a JSTL
variable.
There may be
situations in which we need to pass JSTL String Variable to
Javascript functions. If there are javascript special characters such
as single-quote, double-quote etc., present in the JSTL variable
value, it would result in script error. It is always advisable to
escape such special characters before passing to javascript function.
This code-snippet focusses on writing a custom EL function to address
this issue.
//This class requires Apache commons-lang JAR
//Tag Handler Class for custom EL Function
package util;
import org.apache.commons.lang.StringEscapeUtils;
public class JSEscUtil {
public static String escapeJSString(final String s) {
return StringEscapeUtils.escapeJavaScript(s);
}
}