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

I want to call fn: replace inside EL inside c: out to replace quotation marks.

Does not work

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

since the XML parser stops with the first double quotation mark and does not see the end of the c: cout tag (JSP compilation step error).

Following

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

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

+4
source share
1 answer

Parameterize them with <c:set> .

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

Not tied to a specific question, have you yet considered a real JSON generator? For example, using Gson, this is a question of the following oneliner, given that user is a full-fledged javabei:

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

You will get syntactically valid JSON directly, without confusing all the ways to get JSP / JSTL / EL to create valid JSON.

+5
source

Source: https://habr.com/ru/post/1391502/


All Articles