How to use both single and double quotes inside JSTL/EL expression?

偶尔善良 提交于 2019-12-29 08:45:12

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/8898815/how-to-use-both-single-and-double-quotes-inside-jstl-el-expression

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