如何在JSTL / EL表达式中使用单引号和双引号? [英] How to use both single and double quotes inside JSTL/EL expression?

查看:703
本文介绍了如何在JSTL / EL表达式中使用单引号和双引号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下内容不起作用

 < c:out value =$ {fn:replace(userName,'','\\'')}/> 

因为XML解析器停在第一个双引号,看不到c:cout标签终止(JSP编译阶段错误)



以下

 < c:out value = $ {FN:替换(用户名, '&安培; QUOT;', '&\\放大器; QUOT;')}/> 

不工作,可能是因为替换功能没有看到实际的引号字符。

解决方案

使用< c:set> 参数化

 < c:set var =searchvalue ='''/> 
< c:set var =replacevalue = '\\\''/>
< c:out value =$ {fn:replace(userName,search,replace)}/>






无关具体的问题,你还没有考虑到一个真正的JSON生成器?例如Gson,这是一个关于以下背景的问题,因为用户是一个完整的Javabean:

  String json = new Gson()。toJson(user); 

您将直接获得语法上有效的JSON,而无需使JSP / JSTL / EL生成所有方法有效的JSON。


I want to call fn:replace inside EL inside c:out to replace quote caracters.

The following does not work

<c:out value="${fn:replace(userName,'"','\\"')}"/>

because XML parser stops at first double quote and sees no c:cout tag termination (JSP compilation stage error).

The following

<c:out value="${fn:replace(userName,'&quot;','\\&quot;')}"/>

does not work, probably because replace function does not see actual quote character.

解决方案

Parameterize them with <c:set>.

<c:set var="search" value='"' />
<c:set var="replace" value='\\"' />
<c:out value="${fn:replace(userName, search, replace)}"/>


Unrelated to the concrete question, have you still not considered a real JSON generator? With for example Gson it's a matter of the following oneliner, given that user is a fullworthy Javabean:

String json = new Gson().toJson(user);

You'll get syntactically valid JSON directly without fiddling all ways to get JSP/JSTL/EL to produce valid JSON.

这篇关于如何在JSTL / EL表达式中使用单引号和双引号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆