Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I loop through collections of strings but not strings with commas in JSTL/JSP?
    text
    copied!<p>Here is an excerpt from a jsp file I'm working with:</p> <pre><code>&lt;%@ page session="false" %&gt;&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %&gt; ... &lt;c:forEach var="attr" items="${auth.principal.attributes}" &gt; &lt;c:forEach var="attrVal" items="${attr.value}"&gt; &lt;attribute name="${fn:escapeXml(attr.key)}" value="${fn:escapeXml(attrVal)}"/&gt; &lt;/c:forEach&gt; &lt;/c:forEach&gt; ... </code></pre> <p>Each of the attributes may be either a string or a collection of strings. This code works fine most of the time, but if one of the string values contains a comma then it gets split apart into two attributes.</p> <p>What I'm expecting is output like the following:</p> <pre><code>... &lt;attribute name="FirstName" value="Adam"/&gt; &lt;attribute name="LastName" value="Franco"/&gt; &lt;attribute name="DisplayName" value="Franco, Adam S."/&gt; &lt;attribute name="Status" value="Staff"/&gt; &lt;attribute name="MemberOf" value="CN=All Staff,OU=Groups,DC=example,DC=edu"/&gt; &lt;attribute name="MemberOf" value="CN=Admins,OU=Groups,DC=example,DC=edu"/&gt; ... </code></pre> <p>What I get instead is:</p> <pre><code>... &lt;attribute name="FirstName" value="Adam"/&gt; &lt;attribute name="LastName" value="Franco"/&gt; &lt;attribute name="DisplayName" value="Franco"/&gt; &lt;attribute name="DisplayName" value=" Adam S."/&gt; &lt;attribute name="Status" value="Staff"/&gt; &lt;attribute name="MemberOf" value="CN=All Staff,OU=Groups,DC=example,DC=edu"/&gt; &lt;attribute name="MemberOf" value="CN=Admins,OU=Groups,DC=example,DC=edu"/&gt; ... </code></pre> <p>I've verified that my "DisplayName" attribute is coming through to the from the back-end as a simple string just like FirstName and LastName, the only difference is that it happens to contain a comma. Here is a screen-shot of the the <code>principle.attributes</code> Map right before it gets returned to the JSP: <img src="https://i.stack.imgur.com/glPbN.png" alt="enter image description here"></p> <p>How can I differentiate between a string and a collection of strings in the JSP or prevent the <code>forEach</code> invocation from splitting strings appart on commas? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload