The Java Object Validation Framework |
||
i-screen.org |
Download (SourceForge Hosted) Documentation
Articles Feedback |
OGNL (and MVEL) GuideIntroductioniScreen utilizes the OGNL library for Java object references. It also uses the MVFLEX Expression Language (MVEL). Though this guide describes the use of OGNL, it is basically the same for the MVEL library. The primary difference is that where OGNL uses ${} for embedding expressions, MVEL uses @{}. Both are powerful libraries for accessing objects and object graphs. For a more detailed explanation and guide to the OGNL library, see the OGNL website. For a more detailed explanation of MVEL, see the MVEL website. The BasicsOGNL utilizes an "expression" that defines the object graph that needs to be followed in order to either get or set data. This "expression" assumes a Java instance, called the "root." In iScreen, the actual OGNL root being used by a particular OGNL expression is dependent upon the context. In general, it's either the Java instance that's being validated, or it's a special OGNL root object called ContextBean (the full name is org.iscreen.impl.ContextBean). A simple example of an OGNL expression would be the JavaBean property, such as "myProperty". A more complex expression might reference properties that are "deeper," such as "myProperty.deeperProperty". Referencing data in collections, arrays, maps, static methods, etc. are all possible. See the OGNL website for greater detail on what can be done. Fundamentally, the OGNL expression acts upon the OGNL root to either get a property or set a property on that root. In one example within iScreen (more details later), one OGNL expression acts as a "getter" on one OGNL root, and another expression acts as a "setter" on a different OGNL root. The org.iscreen.impl.ContextBean ObjectThe ContextBean object has a number of properties, some of which are "valid" at different times within the validation process. For exact detail, see the JavaDoc on the ContextBean class. The primary properties of the ContextBean object are:
Where It's UsedThere are two basic places where OGNL can be found: the XML configuration, and within individual messages in resources. OGNL In MessagesOGNL can be used within resources (property files) by embedding OGNL expressions using the ${expression} approach. In all cases, the OGNL root used for messages, whether they are embedded within a properties file or directly within the XML configuration file (but as part of a resource), is the ContextBean object. To embed OGNL expressions in messages, the OGNL expression is embedded within braces, like this: ${OGNL expression}. For example, to embed the label and the failure object within a message, the failure message might look like this: "The ${label} field failed because of ${failure}." Note that the OGNL expression root is the ContextBean object, described above. OGNL In ConfigurationOGNL is used through the XML configuration file for referencing properties of the Java instance being validated, or the properties of the ContextBean object. In addition, it's also used when mapping properties from a Java instance to the beanToValidate prior to validation. Mapping -- The mapping element of the XML configuration is used to map properties on the Java instance (typically a JavaBean) to properties on the "bean to validate" that is created by a Validator prior to validation. The mapping element has two attributes: a 'to' and a 'from' attribute. The 'to' attribute refers to the "bean to validate" object and is a 'setter' OGNL expression with that object as the OGNL root. The 'from' attribute refers to the Java instance being validated, and is a 'getter' OGNL expression on it. The default value for the 'from' attribute is #root, which means the Java instance being validated (itself, not any of its properties). The default value for the 'to' attribute is 'value,' which implies the use of the default org.iscreen.SimpleBean object (it only has one property, which is 'value'). Validation Set Forwarding -- When forwarding to another validation set from within another validation set, there are a couple of attributes on the 'use-validation-set' element that use OGNL expressions. The first is the 'map' attribute, which maps a property (using an OGNL expression) on the Java instance being validated to the Java instance that the forwarded validation set actually acts upon (so, the "parent" validation set acts upon the "parent" Java instance, and the "child" validation set acts upon the "child" Java instance). By default, the 'map' attribute is #root, which means the Java instance, itself, is forwarded. The other attribute is 'if,' which is used as a conditional as to whether the forwarded validation set is actually used or not. The 'if' attribute is an OGNL expression that must be convertable to a boolean value. If true, then the forwarding occurs. If false, it does not. If it's not specified, it defaults to true. The OGNL root for the 'if' attribute is the Java instance being validated (or, more specifically, the Java instance that the validation set is validating). Constraints -- The constraint element has an attribute called 'property' which is an OGNL expression for setting purposes on the Validator. It's used to set a contraint (really, it's just a property) on the Validator as part of its configuration. The ContextBean used in this expression is the Validator instance, itself. Failures -- The failure element is exactly like the contraint element, so it has an attribute called 'property' that acts in the same way that a constraint does. The only real difference between these is to differentiate between what's being done and used. The failure object type is somewhat fixed, where the constraint can be of different types. |
|
|