On 24 Sep 2002, Craig Longman wrote:

> what i want to do is this:
> 
> <c:if test"${not empty param.typeID}">
>  <c:set var="${param.parent_objectName}_typeID" value="${param.typeID}"/>
> </c:if>
> 
> then later:
> 
> <c:out value="${${parent_objectName}_typeID}"/>
> 
> this would store an attribute named either Product_typeID or
> Service_typeID that could then be used elsewhere in the page.
> 
> unfortunately, the problems are plenty.  it seems c:set.var isn't EL
> enabled (there is nothing set by the name of Service_typeID for
> example) and one can't 'double evaluate' in the c:out.value attribute.
> 
> the only way i can see of doing this now, is to create a custom tag
> that performs the magic, which might make sense for me in this case
> anyway, but i'd thought i'd throw this into the pit to be picked
> apart.

You don't have to go that far.  Indeed, you can't use dynamic values with
'var', and you can't begin a subordinate expression within a top-level
expression.  However, using collections, it should be straightforward to
do what you want:

 <%-- Set up a Map.  Of course, a back-end servlet could do this too. --%>
 <jsp:useBean id="types" class="java.util.Map" />

 <c:if test="${not empty param.typeID}">
  <c:set target="${types}"
         property="${param.parent_objectName}_typeID"
         value="${param.typeID}" />
 </c:if>
         
This saves information, which you can later retrieve with

 <c:set var="propertyName" value="${param.parent_objectName}_typeID" />
 <c:out value="${types[propertyName}" />

Note the <c:set>, which lets us build up a complex temporary key into
'types'; you can't concatenate strings in the JSTL EL, so the
concatenation must occur outside an expression -- in this case, in the
attribute value of <c:set>.

Hope that helps,

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


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to