views:

1383

answers:

2

I have an array of values being made available, but unfortunately some of the variable names include a space. I cannot work out how to simply output these in the page. I know I'm not explaining this well (I'm the JSP designer, not the Java coder) so hopefully this example will illustrate what I'm trying to do:

<c:out value="${x}"/>

outputs to the page (artificially wrapped) as:

{width=96.0, orderedheight=160.0, instructions=TEST ONLY. This is a test.,
 productId=10132, publication type=ns, name=John}

I can output the name by using

<c:out value="${x.name}"/>

no problems. The issue is when I try to get the "publication type"... because it has a space, I can't seem to get <c:out> to display it.

I have tried:

<!-- error parsing custom action attribute: -->
<c:out value="${x.publication type}"/>
<!-- error occurred while evaluating custom action attribute: -->
<c:out value="${x.publication+type}"/>
<!-- error occurred while parsing custom action attribute: -->
<c:out value="${x.'publication type'}"/>
<!-- error occurred while parsing custom action attribute: -->
<c:out value="${x.publication%20type}"/>

I know the real solution is to get the variable names formatted correctly (ie: without spaces) but I can't get the code updated for quite a while. Can this be done? Any help greatly appreciated.

+4  A: 

Have you tried:

<c:out value="${x['publication type']}"/>

I am assuming that a Map is the Java type behind this.

Eddie
Beautiful. Thanks for the answer! And in less than 10 minutes. Wow.
Shane
Glad to help. Java Maps have two different access mechanisms in JSTL, as you've learned. But for a Map key that is a String containing a space, you have to use the [] method of access
Eddie
A: 

I'm wondering if the variable name itself contains space or dot, how to output that variable using c:out ?

For example you have a request attribute named "user.name" , how to let c:out resolve the variable correctly?

guigui
Welcome at Stackoverflow! Whenever you have a **question**, please do not post it as an **answer**. Press the `Ask Question` button at the top to ask a question, not the `Post Your Answer` button at the bottom. You should then delete this "answer" using the `delete` link.
BalusC