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 Taavi Tiirik <ta...@ibs.ee> on 2003/07/03 14:23:42 UTC

expression that contains expression

Hi, I have variable names like name1, name2, name3, etc. in JSTL.

Instead of hard coding the expressions like this ${name1}, ${name2}
I would like to use them in a loop.

The following syntax is not valid of course. Is there a way how to
achieve this:

<c:forEach begin="1" end="4" var="index">
    <c:out value="${name${index}}"/>
</c:forEach>

thanks,
Taavi


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


Re: expression that contains expression

Posted by Taavi Tiirik <ta...@ibs.ee>.
Thanks, David

${pageScope[variableName]} is indeed a solution that works. Thanks
for pointing it out. It does not work though if variable contains a dot in
its name as they do in my case.

There is this article providing one solution at
http://www.mail-archive.com/taglibs-user@jakarta.apache.org/msg04616.html

It just gets too tricky. Some sort of custom tag would help but I do not
want custom tags here at this point.

Does jsp 2.0 expression language address this somehow?
${name${index}} could be quite readable and understandable I think :)

with best wishes,
Taavi


From: "David M. Karr" <dm...@earthlink.net>
> This isn't as clean as it could be, but now that you have a variable that
> contains the name of the variable, if you know which scope the variable is
in,
> you can then reference that scope as a HashMap, using the variable value
as the
> key.  If you don't know the scope it is in, well, we don't yet have a
"scope"
> HashMap that is a union of all of the scopes, so you could conceivable
build it
> from scratch.
>
> For instance, the scoped HashMaps are "pageScope", "requestScope",
> "sessionScope", and "applicationScope".  The reference of
> "${pageScope[variableName]}" would reference your computed variable name,
if
> you knew it was in page scope.


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


Re: expression that contains expression

Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Taavi" == Taavi Tiirik <ta...@ibs.ee> writes:

    Taavi> Nope. I do not have them in the array. I need to construct
    Taavi> variable name on the fly.

    Taavi> I could use <c:set> to build a variable name:

    Taavi>     <c:set var="variableName" value="name${index}"/>

    Taavi> but I wonder now if there exists a way how to get a value
    Taavi> of a variable with the name 'variableName'?

This isn't as clean as it could be, but now that you have a variable that
contains the name of the variable, if you know which scope the variable is in,
you can then reference that scope as a HashMap, using the variable value as the
key.  If you don't know the scope it is in, well, we don't yet have a "scope"
HashMap that is a union of all of the scopes, so you could conceivable build it
from scratch.

For instance, the scoped HashMaps are "pageScope", "requestScope",
"sessionScope", and "applicationScope".  The reference of
"${pageScope[variableName]}" would reference your computed variable name, if
you knew it was in page scope.

The "union" HashMap would contain the application, session, request, and page
HashMaps, in that order (for precedence).

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net   ; SCJP; SCWCD




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


Re: expression that contains expression

Posted by Taavi Tiirik <ta...@ibs.ee>.
Nope. I do not have them in the array. I need to construct
variable name on the fly.

I could use <c:set> to build a variable name:

    <c:set var="variableName" value="name${index}"/>

but I wonder now if there exists a way how to get a value
of a variable with the name 'variableName'?

Taavi

> nevermind, don't think you can do it like that.  But it'll probably be
> easier if you put name1, name2, name3, etc into an array, or hash, or
> such, and do:
> 
> >  <c:forEach begin="1" end="4" var="index">
> >      <c:out value='${name[index]}'/>
> >  </c:forEach>
> >
> 
> nick
> On Thu, 3 Jul 2003, N. Chen wrote:
> 
> >
> >
> > well, actually yes.  Assuming name is an array or something similar:
> >
> >  <c:forEach begin="1" end="4" var="index">
> >      <c:out value='${name[index]}'/>
> >  </c:forEach>
> >
> > On Thu, 3 Jul 2003, Taavi Tiirik wrote:
> >
> > >
> > > Hi, I have variable names like name1, name2, name3, etc. in JSTL.
> > >
> > > Instead of hard coding the expressions like this ${name1}, ${name2}
> > > I would like to use them in a loop.
> > >
> > > The following syntax is not valid of course. Is there a way how to
> > > achieve this:
> > >
> > > <c:forEach begin="1" end="4" var="index">
> > >     <c:out value="${name${index}}"/>
> > > </c:forEach>
> > >
> > > thanks,
> > > Taavi


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


Re: expression that contains expression

Posted by "N. Chen" <ni...@u.washington.edu>.
opps. :)

nevermind, don't think you can do it like that.  But it'll probably be
easier if you put name1, name2, name3, etc into an array, or hash, or
such, and do:

>  <c:forEach begin="1" end="4" var="index">
>      <c:out value='${name[index]}'/>
>  </c:forEach>
>

nick
On Thu, 3 Jul 2003, N. Chen wrote:

>
>
> well, actually yes.  Assuming name is an array or something similar:
>
>  <c:forEach begin="1" end="4" var="index">
>      <c:out value='${name[index]}'/>
>  </c:forEach>
>
> On Thu, 3 Jul 2003, Taavi Tiirik wrote:
>
> >
> > Hi, I have variable names like name1, name2, name3, etc. in JSTL.
> >
> > Instead of hard coding the expressions like this ${name1}, ${name2}
> > I would like to use them in a loop.
> >
> > The following syntax is not valid of course. Is there a way how to
> > achieve this:
> >
> > <c:forEach begin="1" end="4" var="index">
> >     <c:out value="${name${index}}"/>
> > </c:forEach>
> >
> > thanks,
> > Taavi
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>


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


Re: expression that contains expression

Posted by "N. Chen" <ni...@u.washington.edu>.

well, actually yes.  Assuming name is an array or something similar:

 <c:forEach begin="1" end="4" var="index">
     <c:out value='${name[index]}'/>
 </c:forEach>

On Thu, 3 Jul 2003, Taavi Tiirik wrote:

>
> Hi, I have variable names like name1, name2, name3, etc. in JSTL.
>
> Instead of hard coding the expressions like this ${name1}, ${name2}
> I would like to use them in a loop.
>
> The following syntax is not valid of course. Is there a way how to
> achieve this:
>
> <c:forEach begin="1" end="4" var="index">
>     <c:out value="${name${index}}"/>
> </c:forEach>
>
> thanks,
> Taavi
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: taglibs-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: taglibs-user-help@jakarta.apache.org
>
>


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