Escape apostrophe as \' with c:out JSP

前端 未结 2 1839
忘掉有多难
忘掉有多难 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:

    <function>
     <name>escapeJs</name>
     <function-class>package.to.MyStringUtil</function-class>
     <function-signature>
       java.lang.String escapeJs( java.lang.String )
     </function-signature>
    </function>
    

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

    <a href="#" 
      class="delete" 
      onclick="if(confirm('${foo:escapeJs(person.lastName)}' + _('Are you sure you want to delete this application?'))) {deleteApplication('${application.identifier}')};return false;"><bean:message key="application.delete"/></a>
    
    0 讨论(0)
  • 2021-01-20 10:59

    Store it as O'brian in the database, but before displaying it do a find replace to convert any ' to \'

    0 讨论(0)
提交回复
热议问题