How to insert a space between two variables in JSP? [duplicate]
Asked Answered
E

2

5

I have one line of html in a form on a JSP page like this:

<c:forEach var="entry" items="${set}">
    <input type="checkbox" name="thing" value="${entry.key} ${entry.value}">
</c:forEach>

However, the space between the key and the value ${entry.key} ${entry.value} is lost when the form is submitted. I've tried using a \ before the , in that case both the \ and the are still there when submitting.

It seems that Java EL does not preserve isolated spaces, is that right? If so, is there a valid workaround?

EDIT: This is so silly of me, many thanks to Cthulhu. But I still don't understand the behavior after adding a \. Why would that cause the space to show?

Ecstatic answered 13/11, 2012 at 20:6 Comment(0)
S
12

You can insert white-spaces using: &nbsp;

Character Entity References

Edit: Seems to be a bug in the way spaces are handled by the EL parser, so you should use either html entities like &nbsp; or the other ways outlined here.

Sawn answered 13/11, 2012 at 20:7 Comment(6)
Keep talking nonsense recently:( Thanks!Ecstatic
Sidenote: why inserting a \ changes the behavior of the space? Thanks!Ecstatic
But the whitespace is not ignored once the \ is inserted, and that's part of the reason that I didn't try &nbsp;, since I assumed it was the JSP compiler that did some tricks in there.Ecstatic
@ZiyaoWei Seems to be a bug in the way the EL parser does processing. https://mcmap.net/q/1269925/-how-do-i-make-jsp-tag-files-not-ignore-all-whitespaceSawn
Ha, that solves it completely. Thanks for digging up that post!Ecstatic
Note that &nbsp; is the entity for a non-breaking space. That’s not the same as entering a regular space character (it’ll stop the word after it from wrapping onto the next line), and doesn’t work if you’re trying to output your values into an HTML attribute like class.Lind
C
4
${entry.key}${' '}${entry.value}

was my way to fix this. Seems a little less verbose than the

<c:out value="${bean.foo} ${bean.bar} ${bean.waa}" />

from the other thread.

Continuator answered 22/12, 2014 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.