Working with the Java Expression Language on a Page

I am working on a project interface that gives me Language Expression Language tags to work with. In one case, I need to see if it returns an array or only 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>

The problem is that I need to output more if there are more buildings in the bits:

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 creating this code, and they said, "Dunnokindofbusyrightnowbuhbye." so I hope you have some understanding.

, , Java Expression Language ( , , ). / .


EDIT:

:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

:

<c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'>
  <p></p>
</c:forEach>

, , Java Expression Language, , items = '' , , , . , :

<p>${WebAppContext.buildings[0].location.name}</p>
<p>${WebAppContext.buildings[1].location.name}</p>

:

<p>Krustylu Studios</p>
<p>Springfield Nuclear Power Plant</p>
+5
4

, EL; JSTL c: forEach .

+4

<c:forEach var='meeting' items='[Lorg.foo.bar.baz.bat.serviceapi.webserviceobject.xsd.BuildingsWSO;@3823ff8'> , , <c:forEach> .

, JSP:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

prefix="c" , c: <c:forEach>... prefix="foo", <foo:forEach>

+3

:

<c:forEach var='building' items='${WebAppContext.buildings}'>
  <p>${building.location.name}</p>
</c:forEach>

, , WebAppContext.buildings ? JSTL, EL JSTL.

+2

, , JSP-.

public static boolean isArray(final Object o) {
    return o instanceof Object[];
}

Then just map it to a TLD, for example:

<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>
+1
source

All Articles