Escape apostrophe as \' with c:out JSP

前端 未结 2 1840
忘掉有多难
忘掉有多难 2021-01-20 10:25

I have an object field with person\'s last name.

If I use ${person.lastName}, I get O\'Brian

If I use

 

        
2条回答
  •  面向向阳花
    2021-01-20 10:58

    You could define a new EL function that escapes strings for you.

    E.g.

    In Java

    public class MyStringUtil {
      public static String escapeJs( String str )
      {
        // escape the string (e.g. replace ' with \')
      }
    }
    

    In a tag library descriptor file:

    
     escapeJs
     package.to.MyStringUtil
     
       java.lang.String escapeJs( java.lang.String )
     
    
    

    Then in your JSP (assuming you've included your .tld with a prefix of foo:

    
    

提交回复
热议问题