You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by zze-JEANJEAN S ext FTRD/DMI/SOP <sj...@rd.francetelecom.com> on 2002/09/16 19:08:42 UTC

Indirect index (through a variable) in a Map

Hi everybody !

I would like to use something like that :
<c:out value="${pageScope[theKey]}"/>

The result is empty :((

My complete code :
<% String avar = "hello";
String theKey = "avar";
 %>
Direct index :
<c:out value="${pageScope['avar']}"/><BR/>
Indirect index :
<c:out value="${pageScope[theKey]}"/><BR/>

The message for the first c:out is hello, but for the second one : nothing :((

Thanks for your help !!

Stéphane

I read in the archive :

On Sat, 3 Aug 2002, petra staub wrote:

> great, and just another question to this:
> 
> how to do, if the key is a variable, say
> 
> color="blue" and now i want to do the
> same again? how can i make the expression
> first disolve the variable color to "blue"
> 
> color = "blue"
> ${MyMap.color}  -->  sky

It would be ${MyMap[color]} in that case.

-- 
Shawn Bayern
"JSTL in Action"   http://www.jstlbook.com

Re: Indirect index (through a variable) in a Map

Posted by Shawn Bayern <ba...@essentially.net>.
On Mon, 16 Sep 2002, zze-JEANJEAN S ext FTRD/DMI/SOP wrote:

> Hi everybody !
> 
> I would like to use something like that :
> <c:out value="${pageScope[theKey]}"/>
> 
> The result is empty :((
> 
> My complete code :
> <% String avar = "hello";
> String theKey = "avar";
>  %>
> Direct index :
> <c:out value="${pageScope['avar']}"/><BR/>
> Indirect index :
> <c:out value="${pageScope[theKey]}"/><BR/>
> 
> The message for the first c:out is hello, but for the second one :
> nothing :((
> 
> Thanks for your help !!

In principle, what you're doing is almost okay, except for one problem:  
scripting variables are not the same thing as scoped attributes, so
declaring 'theKey' in Java has no effect on the expression language.  If
you wrote

  <c:set var="theKey">
    avar
  </c:set>

then you would be able to use 'theKey' as an index.  Alternatively, if 
you really want to use Java, you could add

  <% pageContext.setAttribute("theKey", theKey); %>

after your Java declaration.

Note that, in either of those circumstances, ${pageScope[theKey]} would be
equivalent to ${pageScope['avar']}.  This does *NOT* find the scripting
variable named 'avar'; you'd have to declare that too as a scoped
attribute with <c:set> or PageContext.setAttribute().

-- 
Shawn Bayern
"JSTL in Action"   http://www.jstlbook.com


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>