How to use both single and double quotes inside JSTL/EL expression?
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.
Dims
Software developer & Machine Learning engineer C/C++/Java/C#/Python/Mathematica/MATLAB/Kotlin/R/PHP/JavaScript/SQL/HTML/ LinkedIn: http://www.linkedin.com/in/dimskraft Telegram: https://t.me/dims12 I prefer fishing rod over fish.
Updated on June 04, 2022Comments
-
Dims almost 2 years
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,'"','\\"')}"/>
does not work, probably because replace function does not see actual quote character.
-
Dims over 12 yearsYes, thanks will probably use it. Was wondering anyway if JSTL/EL has such a feature lack.
-
BalusC over 12 yearsMaybe you're just using the wrong tool for the job ;) JSP is merely a view technology designed to generate HTML dynamically. JSTL/EL are streamlined to suit that purpose.
-
Dims over 12 yearsHm... Sometimes I generate JSONs in iterating loop... I am sure Gson can handle this but am not feel it will be beautiful to collect data in temporary objects before sending... Also I am generating not only json but xml also... hence it was an idea to have a bunch of JSPs for all these interactions...
-
Dims over 12 yearsI don't think this is my fault that they didn't think how to escape quotes in quotes :) Also if JSP is for html only then what for they have created a content-type tag?
-
BalusC over 12 yearsAlso XML? For that JAX-RS was invented. Caching can just be delegated to persistence layer.
-
Dims over 12 yearsSo what advantages over JSPs it has in situation like mine -- to have few adaptor services for different clients?
-
BalusC over 12 yearsCheck the JAX-RS example in this answer: stackoverflow.com/questions/7874695/servlet-vs-restful/…
-
Dims over 12 yearsIt looks good if all sides are under my control. For the case I need create predefined textual representation from predefined java objects it looks overcomplexed.