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 Er...@swissinfo.ch on 2003/04/16 11:45:25 UTC

Finding out what kind of collection there is

Hi

I'm trying to write a couple of generic JSPs that show our objects. There is
either a request variable "object" or a request variable "objects".
My JSP "show_objects.jsp" gets the one object or iterates over "objects" -
both use "show_object.jsp" in the end.

The problem I have with "objects" (a collection) is that I don't know
whether it's a simple ArrayList or a HashMap.

For ArrayList, this would be:
<c:forEach var="singleObject" items="${objects}" varStatus="status">
	<c:set var="object" value="${singleObject}" scope="request"/>
	<c:import url="/jsp/include/show_object.jsp"/>
</c:forEach>

For HashMap, this would be:
<c:forEach var="singleObject" items="${objects}" varStatus="status">
	<c:set var="object" value="${singleObject.value}" scope="request"/>
	<c:import url="/jsp/include/show_object.jsp"/>
</c:forEach>

However, how can I find out what kind of collection this is?
If I use:

<c:forEach var="singleObject" items="${objects}" varStatus="status">
	<c:choose>
		<%-- HashMap --%>
		<c:when test="${! empty singleObject.key && ! empty
singleObject.value}">
			<c:set var="object" value="${singleObject.value}"
scope="request"/>
		</c:when>
		<%-- ArrayList --%>
		<c:otherwise>
			<c:set var="object" value="${singleObject}"
scope="request"/>
		</c:otherwise>
	</c:choose>
	<c:import url="/jsp/include/show_object.jsp"/>
</c:forEach>

I get an Exception, because my single objects in the ArrayList don't
implement getKey() or getValue().

So I need some kind of "instanceof" in JSTL.

Does anyone have an idea how to do this?

Thanks & best regards,
Eric


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


Re: Finding out what kind of collection there is

Posted by Henri Yandell <ba...@generationjava.com>.
Of course, it would help if this was noticeable from the website
somewhere. Currently unstandard has two tags from Tim O'Brien, an
InstanceOf tag and an Equals tag which can handle equalsIgnoreCase for
Strings.

I'll try to resolve this.

Hen

On Wed, 16 Apr 2003, Henri Yandell wrote:

>
> One option is the un:instanceOf tag in jakarta-taglibs-sandbox/unstandard.
>
> Would be interesting to hear if it is able to solve the lack of instanceof
> problem for you.
>
> Hen
>
> On Wed, 16 Apr 2003 Eric.Lewis@swissinfo.ch wrote:
>
> > Hi
> >
> > I'm trying to write a couple of generic JSPs that show our objects. There is
> > either a request variable "object" or a request variable "objects".
> > My JSP "show_objects.jsp" gets the one object or iterates over "objects" -
> > both use "show_object.jsp" in the end.
> >
> > The problem I have with "objects" (a collection) is that I don't know
> > whether it's a simple ArrayList or a HashMap.
> >
> > For ArrayList, this would be:
> > <c:forEach var="singleObject" items="${objects}" varStatus="status">
> > 	<c:set var="object" value="${singleObject}" scope="request"/>
> > 	<c:import url="/jsp/include/show_object.jsp"/>
> > </c:forEach>
> >
> > For HashMap, this would be:
> > <c:forEach var="singleObject" items="${objects}" varStatus="status">
> > 	<c:set var="object" value="${singleObject.value}" scope="request"/>
> > 	<c:import url="/jsp/include/show_object.jsp"/>
> > </c:forEach>
> >
> > However, how can I find out what kind of collection this is?
> > If I use:
> >
> > <c:forEach var="singleObject" items="${objects}" varStatus="status">
> > 	<c:choose>
> > 		<%-- HashMap --%>
> > 		<c:when test="${! empty singleObject.key && ! empty
> > singleObject.value}">
> > 			<c:set var="object" value="${singleObject.value}"
> > scope="request"/>
> > 		</c:when>
> > 		<%-- ArrayList --%>
> > 		<c:otherwise>
> > 			<c:set var="object" value="${singleObject}"
> > scope="request"/>
> > 		</c:otherwise>
> > 	</c:choose>
> > 	<c:import url="/jsp/include/show_object.jsp"/>
> > </c:forEach>
> >
> > I get an Exception, because my single objects in the ArrayList don't
> > implement getKey() or getValue().
> >
> > So I need some kind of "instanceof" in JSTL.
> >
> > Does anyone have an idea how to do this?
> >
> > Thanks & best regards,
> > Eric
> >
> >
> > ---------------------------------------------------------------------
> > 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: Finding out what kind of collection there is

Posted by Henri Yandell <ba...@generationjava.com>.
One option is the un:instanceOf tag in jakarta-taglibs-sandbox/unstandard.

Would be interesting to hear if it is able to solve the lack of instanceof
problem for you.

Hen

On Wed, 16 Apr 2003 Eric.Lewis@swissinfo.ch wrote:

> Hi
>
> I'm trying to write a couple of generic JSPs that show our objects. There is
> either a request variable "object" or a request variable "objects".
> My JSP "show_objects.jsp" gets the one object or iterates over "objects" -
> both use "show_object.jsp" in the end.
>
> The problem I have with "objects" (a collection) is that I don't know
> whether it's a simple ArrayList or a HashMap.
>
> For ArrayList, this would be:
> <c:forEach var="singleObject" items="${objects}" varStatus="status">
> 	<c:set var="object" value="${singleObject}" scope="request"/>
> 	<c:import url="/jsp/include/show_object.jsp"/>
> </c:forEach>
>
> For HashMap, this would be:
> <c:forEach var="singleObject" items="${objects}" varStatus="status">
> 	<c:set var="object" value="${singleObject.value}" scope="request"/>
> 	<c:import url="/jsp/include/show_object.jsp"/>
> </c:forEach>
>
> However, how can I find out what kind of collection this is?
> If I use:
>
> <c:forEach var="singleObject" items="${objects}" varStatus="status">
> 	<c:choose>
> 		<%-- HashMap --%>
> 		<c:when test="${! empty singleObject.key && ! empty
> singleObject.value}">
> 			<c:set var="object" value="${singleObject.value}"
> scope="request"/>
> 		</c:when>
> 		<%-- ArrayList --%>
> 		<c:otherwise>
> 			<c:set var="object" value="${singleObject}"
> scope="request"/>
> 		</c:otherwise>
> 	</c:choose>
> 	<c:import url="/jsp/include/show_object.jsp"/>
> </c:forEach>
>
> I get an Exception, because my single objects in the ArrayList don't
> implement getKey() or getValue().
>
> So I need some kind of "instanceof" in JSTL.
>
> Does anyone have an idea how to do this?
>
> Thanks & best regards,
> Eric
>
>
> ---------------------------------------------------------------------
> 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