0

I have been trying to get the contents of the comma separated string like

"RAVI, PRASHANT, ANKIT, PANKAJ, Nilesh Kumar Mishra, Anoop Kumar"

Each element in this list needs to be displayed in the dropdown menu in a JSP page. The JSP code doing this bit goes like this:

<select class="style1" id="id" placeholder="sample placeholder" >
    <c:forEach var="name" items="${fn:split(names, ',')}">
        <option value="${name}">${name}</option>
    </c:forEach>
</select>

But after the page is rendered i notice that if output for ${name} contains any whitespace the words after the whitespace are getting rendered as the attributes inside option tag.

<select class="style1" id="id" placeholder="sample placeholder" >
    <option value="RAVI">RAVI</option>
    <option value="PRASHANT">PRASHANT</option>
    <option value="ANKIT">ANKIT</option>
    <option value="PANKAJ">PANKAJ</option>
    <option value="Nilesh" kumar="" mishra="">Nilesh Kumar Mishra</option>
    <option value="Anoop" kumar="">Anoop Kumar</option>
</select>

I tried some searching for the solution and came across with the SO link. But solution provided is for the PHP and also i am unable to find the equivalent for htmlspecialchars in JSP.

Can anybody help me in avoiding those unintended attributes and instead get the whole string as a value for 'value' attribute in the option tag, just like the first three are getting rendered?

Thanks in advance.

Community
  • 1
  • 1
Jeet Prakash
  • 625
  • 1
  • 7
  • 13
  • try this: and see what happens – The Guest Apr 14 '15 at 19:01
  • To escape XML special characters, an analogous JSTL function which may be supposed is `${fn:escapeXml(name)}` or while outputting, `` which is implicit (i.e. `escapeXml` is set to true, by default) but this is unlikely to help in this case. What does `names` contain, a comma-separated list of Strings for sure? – Tiny Apr 14 '15 at 19:02
  • @Tiny, yes the `names` contains the comma separated list of strings like this: `"RAVI, PRASHANT, ANKIT, PANKAJ, Nilesh Kumar Mishra, Anoop Kumar"` – Jeet Prakash Apr 15 '15 at 04:27

2 Answers2

0

I bet your comma delimited string has quotes in it when the data has a white space, like this:

RAVI, PRASHANT, ANKIT, PANKAJ, "Nilesh Kumar Mishra", "Anoop Kumar"

Then when the foreach runs the quotes are prematurely terminating the value attribute. So you end up with this ...

<option value=""nilesh kumar mishra"" ...

instead of this ...

<option value="nilesh kumar mishra" ...

And the browser ends up interpreting each of those strings as an attribute instead of the data for the value attribute. So you need to get rid of the quotes like this:

<select class="style1" id="id" placeholder="sample placeholder" >
    <c:forEach var="name" items="${fn:split(names, ',')}">
        <option value="${fn:replace(name, '"', '')}">${name}</option>
    </c:forEach>
</select>
alfreema
  • 1,308
  • 14
  • 26
  • Sorry for not including that part in the question itself but first we are declaring the string in the controller like this: private final String names = "RAVI, PRASHANT, ANKIT, PANKAJ, Nilesh Kumar Mishra, Anoop Kumar"; and later setting it in the model to be picked by JSP like this: model.addAttribute("names", names); – Jeet Prakash Apr 15 '15 at 04:21
0

Given your situation, here is probably the easiest way to deal with it ... a) replace all the spaces with |s, b) split the string, c) replace the |s back to spaces. Here's how to do that ...

<select class="style1" id="id" placeholder="sample placeholder" >
    <c:forEach var="name" items="${fn:split(fn:replace(names, ' ', '|'), ',')}">
        <option value="${fn:replace(name, '|', ' ')}">${name}</option>
    </c:forEach>
</select>

this will make sure that the split string is one continuous string, yet the output is formatted as it was originally (with spaces).

alfreema
  • 1,308
  • 14
  • 26