<--PREVIOUS | NEXT---> |
WORKING WITH JAVA COLLECTIONS
Java Collections are a mainstay of the Java web developer’s daily workload. While the JavaBeans specification has always supported indexed properties, working with actual Java Collections, while convenient in the Java side, has always been a hassle in contexts such as JSP tags. One of the great things about the OGNL expression language is its simplified handling of Collections. We’ll now summarize the OGNL syntax used to reference these properties.
WORKING WITH LISTS AND ARRAYS
References to lists and arrays share the same syntax in OGNL. Table below summarizes the basic syntax to access list or array properties.
As the table demonstrates, the syntax for referencing elements or properties of
lists and arrays is intuitive. Basically, OGNL uses array index syntax for both. This
makes perfect sense, due to the ordered, indexed nature of lists.
A couple of things warrant remarks. First, the reference to the name property of a list element assumes something important. As we know, Java Lists are type-agnostic. In Java, we always have to cast the element to the appropriate type, in this case User, before we try to reference the name property. This syntax assumes that has been done. We should also note that you can reference other properties, such as length and size, of arrays and lists. In particular, note that OGNL makes the List class’s non-JavaBeans-conformant size method answer to a simple property reference. This is something nice that OGNL provides as free service to its valued customers!
OGNL also allows you to create List literals. This can be useful if you want to directly create a set of values to feed to something like a select box. Table below shows the syntax for creating these literals.
You probably only want to do this with trivial data, since creating complex data in the view layer would make a mess. Nonetheless, sometimes this will be the perfect tool for the job.
<--PREVIOUS | NEXT---> |
No comments:
Post a Comment