Reputation: 20977
I am working on the frontend of a project that gives me Java Expression Language tags to work with. In one instance I need to see if it is returning an array or just one bit of data and I don't know how to work with it.
Example:
The page has
<p>${WebAppContext.buildings[0].location.name}</p>
which will output something like:
<p>Acme</p>
Problem is I need to output more if there is more in that buildings bit:
Something like (in pseudocode):
if isArray(${WebAppContext.buildings}){
foreach(${WebAppContext.buildings} as foo){
//iterate over whatever is in the array
}
}
so I can output something like:
<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>
I asked the Java people responsible for generating this code and they said "Dunnokindofbusyrightnowbuhbye." so I'm hoping you folks will have some insight.
Beyond sticking the code in the page I don't have a clue how to work with this Java Expression Language (I even had to look it up to see what the heck it is called). So any advice/resources would be helpful.
EDIT:
I've tried the following and am not getting any results:
<c:forEach var='building' items='${WebAppContext.buildings}'>
<p>${building.location.name}</p>
</c:forEach>
In the source of the page it just shows:
<c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'>
<p></p>
</c:forEach>
I'll admit that, not knowing anything about Java Expression Language, I don't understand why the items='' gets translated like it does though I can see that it follows a path in the setup we use. Now when I use:
<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>
I get:
<p>Krustylu Studios</p>
<p>Springfield Nuclear Power Plant</p>
Upvotes: 5
Views: 3143
Reputation: 11055
If you really need to know if an object is an array, you could create a custom JSP function.
public static boolean isArray(final Object o) {
return o instanceof Object[];
}
Then just map it in a TLD such as:
<function>
<description>
Checks if the supplied object is an array.
</description>
<name>isArray</name>
<function-class>com.example.JspFunctions</function-class>
<function-signature>boolean isArray(java.lang.Object)</function-signature>
<example>
${f:isArray(someVar)}
</example>
</function>
Upvotes: 1
Reputation: 14800
If you're seeing <c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'>
when you view page source from your browser, that implies that the <c:forEach>
tag isn't being processed.
Make sure you've declared the tag library in the JSP page:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
The prefix="c"
is where you get the c:
part of <c:forEach>
... if you were to say prefix="foo"
the tag would then be <foo:forEach>
Upvotes: 3
Reputation: 413712
This should be possible:
<c:forEach var='building' items='${WebAppContext.buildings}'>
<p>${building.location.name}</p>
</c:forEach>
Now, how will you check to see whether WebAppContext.buildings
is actually an array? There's no direct easy way to do that with JSTL unless you have the power and ability to extend a local custom suite of EL functions to use from JSTL.
Upvotes: 2
Reputation: 13727
I don't think that kind of advanced functionality is supported by EL; you can try using the JSTL c:forEach tag to iterate over your list.
Upvotes: 4