tags:

views:

159

answers:

2

The problem i am having is like this there is variable

<core:set var="type">*one of: load,migrate, or ...* </core:set>

and the value of load, migrate, is a map. Now, i want to print the value of these based on the type? Is it possible at all?

+1  A: 

if they are not in a map, its hard.

I assume you wanted to do it the way perl works: where you could type

$foo = "stuff";
$varName = "foo";
print $$varName; #prints "stuff"

That doesnt work in jsp.

If its a map,you can do ${mapValue[key]}. Info on this page near the Variables section

Chii
All the variables in a JSP are in the equivalent of a map - the javax.servlet.jsp.PageContext, which provides the methods you'll need to query the variable names and their values for each scope.
Nick Holt
you are correct, i wanted the way u did this in perl, also the value of *stuff* is map, **suff** is not in any map.
Rakesh Juyal
+1  A: 

This will achieve the same effect as @Chii's answer:

<c:set var="attributeName" value="foo"/>

<%
  out.println(pageContext.getAttribute(attributeName) + " = " + pageContext.getAttribute(pageContext.getAttribute(attributeName)));
%>

This, nasty as it is, will list all the attributes in the page scope if you need to do that:

<%
  for (String attributeName : pageContext.getAttributeNamesInScope(PageContext.PAGE_SCOPE))
  {
    out.println(attributeName + " = " + pageContext.getAttribute(attributeName));
  }
%>

Don't think there's a way to do this in JSTL, but you normally only need this during debugging so I don't have such a problem with the scriptlet code.

Nick Holt
ah man, please dont go there ! my eyes bleed!!
Chii
@Chii - yeah, scriptlets aren't the prettiest of things, but you can always write a custom tag to hide it all :-)
Nick Holt