You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matthew Van Horn <ho...@accesstech.com> on 2003/05/28 08:45:10 UTC

Expression Language question

How do you write an index variable in an expression, where the index is 
also variable.
i.e.
Given:
HashMap foo = new HashMap();
foo.put("bar", "xyzzy");
foo.put("baz", "plugh");

String indexStr = "bar";


this doesn't seem to work.
<c:out value="${ foo[indexStr] }"/>
I think it is doing foo.get("indexStr") instead of foo.get("bar");


How can I do this the right way?

Thanks,
Matt


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Expression Language question

Posted by Kris Schneider <kr...@dotech.com>.
Doing it all in a JSP:

<jsp:useBean id="foo" class="java.util.HashMap"/>
<c:set target="${foo}" property="bar" value="xyzzy"/>
<c:set target="${foo}" property="baz" value="plugh"/>
<c:set var="indexStr" value="bar"/>
<c:out value="${foo[indexStr]}"/>

Outputs "xyzzy". If you're trying to use a scripting variable with JSTL, it
won't work (unless you're using the RT versions of the tags). The EL versions of
the tags only deal with scoped attributes. So, it would work if you did:

request.setAttribute("indexStr", "bar");

Quoting Matthew Van Horn <ho...@accesstech.com>:

> 
> How do you write an index variable in an expression, where the index is 
> also variable.
> i.e.
> Given:
> HashMap foo = new HashMap();
> foo.put("bar", "xyzzy");
> foo.put("baz", "plugh");
> 
> String indexStr = "bar";
> 
> 
> this doesn't seem to work.
> <c:out value="${ foo[indexStr] }"/>
> I think it is doing foo.get("indexStr") instead of foo.get("bar");
> 
> 
> How can I do this the right way?
> 
> Thanks,
> Matt


-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org