Escape apostrophe as \' with c:out JSP

妖精的绣舞 提交于 2019-12-20 02:29:26

问题


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

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

If I use

 <c:out value="${person.lastName}"/> 

I get O'Brian

Both outputs breaks the next jsp code in IE

 <a href="#" 
    class="delete" 
    onclick="if(confirm('<c:out value="${application.lastName}"/> ' + _('Are you sure you want to delete this application?'))) {deleteApplication('${application.identifier}')};return false;"><bean:message key="application.delete"/></a>

because it gets transformed to

    onclick="if(confirm('O&#039;Brian '

or

    onclick="if(confirm('O'Brian '

I would need O'Brian to be escaped as O\'Brian

Any idea how to solve this issue?

SOLUTION

The most elegant solutions seems to use a simple Tag.

package view;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class EscapeJS extends SimpleTagSupport {
    public String str;

    public void doTag() throws JspException, IOException {
        getJspContext().getOut().print(str.replaceAll("\'", "\\\\'"));
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

}

Then place in WEB-INF a utils.tld file:

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
    <tlibversion>1.2</tlibversion>
    <jspversion>1.1</jspversion>
    <shortname>bean</shortname><uri>utilsTags</uri>
    <uri>utilsTags</uri>
    <tag>
        <name>escapeJS</name>
        <tagclass>view.EscapeJS</tagclass>
        <bodycontent>scriptless</bodycontent>
        <attribute>
            <name>str</name>
            <required>true</required>       
            <rtexprvalue>true</rtexprvalue> 
        </attribute>                
    </tag>      
</taglib>

Then inside your jsp:

<%@ taglib prefix="utils" uri="utilsTags" %>

<utils:escapeJS str="${application.firstName}"/>

回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/1019618/escape-apostrophe-as-with-cout-jsp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!