skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, October 13, 2012

Custom EL Function to escape JS Special Characters

Posted by Raju Gupta at 7:00 AM – 0 comments
 

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);   
    }   
} 


Labels: JSTL Example

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger