简体   繁体   English

如何在JSP中转义单引号

[英]How to escape single quotes in JSP

In my application I have a jsp page where the variables from server is assigned to the angular in client side. 在我的应用程序中,我有一个jsp页面,其中服务器中的变量分配给客户端的角度。 Please see the code below. 请参见下面的代码。

jsp jsp

    <div ng-controller="HeaderController" 
         ng-init="init( {   firstName: '${customerInfo.firstName}',
                            lastName: '${customerInfo.lastName}' }) >

Issue 问题

If the name contains a single quotes, for example, if the firstName is d'souza , the code will be like this firstName: 'd'souza' which breaks. 如果名称包含单引号,例如,如果firstName是d'souza ,则代码将类似于以下firstName: 'd'souza' ,然后中断。 So how can this be escaped in JSP. 那么如何在JSP中对此进行转义。 Something like 就像是

{   firstName: 'escape(${customerInfo.firstName})',
                            lastName: '${customerInfo.lastName}' }

Simply use escapeJavaScript method of org.apache.commons.lang.StringEscapeUtils class. 只需使用org.apache.commons.lang.StringEscapeUtils类的escapeJavaScript方法。 You have to include the common-lang library in your project, obviously. 显然,您必须在项目中包含common-lang库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM