Search from Struts2 Library

Thursday, September 25, 2008

OGNL 4

<--PREVIOUS NEXT--->


WORKING WITH MAPS
OGNL also makes referencing properties and elements of maps delightfully simple.
Table below shows a variety of syntax idioms for referencing Map elements and properties.


As you can see, you can do a lot with maps. The main difference here is that, unlike Lists, the value in the index box must be an object. If the value in the box is some sort of numeric data that would map to a Java primitive, such as an int, then OGNL automatically converts that to an appropriate wrapper type object, such as an Integer, to use as the key. If a string literal is placed in the box, that becomes a string object which will be used for the key. The last row in the table shows a special syntax for maps with strings as keys. If the key is a string, you may use this simpler, JavaBeans-style property notation.
As for other object types that you might use as a key, you ultimately have the full power of OGNL to reference objects that might serve as the key. The possibilities are beyond the capacity of the table format. Note that, as with the List syntax, the direct reference to the name property on the uncast map element depends on the configuration of the OGNL type conversion to know the specific element type of the map.
You can also create Maps on the fly with the OGNL literal syntax. Table below demonstrates this flexible feature.


As you can see, the syntax for creating a Map literal is similar to that for creating a List literal. The main difference is the use of the # sign before the leading brace.

Warning

OGNL uses the # sign in a few different ways. Each is distinct. The uses are completely orthogonal, so you shouldn’t be confused as long as you’re alert to the fact that they’re different use cases. In particular, this isn’t the same use of the # sign as we saw when specifying a nonroot object from the ActionContext for an expression to resolve against. We’ll also see another use of the # sign in a few moments.

Dynamic maps are especially useful for radio groups and select tags. The Struts 2 tag libraries come with special tags for creating user interface components. Just note that you can use literal maps to feed values into some of the UI components. If you wanted to offer a true/false selection that displays as a Yes/No choice, #{true : 'Yes', false : 'No'} would be the value for the list attribute. The value for the value attribute would evaluate to either true or false.


FILTERING AND PROJECTING COLLECTIONS
OGNL supports a couple of special operations that you can conduct on your collections. Filtering allows you to take a collection of objects and filter them according to some rule. For instance, you could take a set of users and filter them down to only those who’re more than 20 years old. Projection, on the other hand, allows you to transform a collection of objects according to some rule. For instance, you could take a set of user objects, having both first and last name properties, and transform it into a set of String objects that combines the first and last name of each user into a single string. To clarify, filtering takes a Collection of size N and produces a new collection containing a subset of those elements ranging from size 0 to size N. On the other
hand, projecting always produces a Collection with exactly the same number of elements as the original Collection; projecting produces a one-for-one result set.

The syntax for filtering is as follows:
collectionName.{? expression }

In the expression, you can use #this to refer to the object from the collection being evaluated. This is another distinct use of the # sign. The syntax for projection is as follows:
collectionName.{ expression }

Table below shows some examples of both of these useful operations in action.


As you can see, each filtering or projection simply returns a new collection for your use. This convenient notation can be used to get the most out of a single set of data. Note that you can combine filtering and projection operations. That about covers it for aspects of OGNL that are commonly used in Struts 2. In the next section, we’ll cover some of the advanced features that might help you out in a pinch, but, still, we recommend keeping it simple unless you have no choice.

<--PREVIOUS NEXT--->

No comments: