Search from Struts2 Library

Monday, September 22, 2008

OGNL 2

<--PREVIOUS NEXT--->

Expression language features commonly used in Struts 2
First, we should review the most common uses of the OGNL expression language in Struts 2 development. In this section, we’ll look at how the expression language serves its purpose for most daily development. Basically, we use it to map the incoming data onto your ValueStack objects, and we use it in tags to pull the data off of the ValueStack while rendering the view. Let’s look at the expression language features most commonly used in this work.

REFERENCING BEAN PROPERTIES
First of all, we need to define what makes an expression. The OGNL expression language refers to something called a chain of properties. This concept is simple.
Take the following expression:
person.father.father.firstName

This property chain consists of a chain of four properties. We can say that this chain references, or targets, the firstName property of person’s grandfather. You can use this same reference both for setting and getting the value of this property, depending on your context.

SETTING OR GETTING?
When we use OGNL expressions to name our form input parameters, we’re referring to a property that we’d like to have set for us. The following code snippet shows the form from our Struts 2 Portfolio application’s Registration.jsp page:

<s:form action="Register">
<s:textfield name="username" label="Username"/>
<s:password name="password" label="Password"/>
<s:textfield name="portfolioName" label="Enter a portfolio name."/>
<s:submit/>
</s:form>

The name of each input field is an OGNL expression. These expressions refer to, for example, the username property exposed on the root OGNL object. As we’ve just learned, the root object is our ValueStack, which probably contains our action object and perhaps a model object. When the params interceptor fires, it’ll take this expression and use it to locate the property onto which it should set the value associated with this name. It’ll also use the OGNL type converters to convert the value from a string to the native type of the target property. There is one common complication that arises when the framework moves data onto the properties targeted by the OGNL expressions.
Take the deeper expression:
user.portfolio.name

If a request parameter targets this property, its value will be moved onto the name property of the portfolio object. One problem that can occur during runtime is a null value for one of the intermediate properties in the expression chain. For instance, what if the user hasn’t been created yet? If you recall, we’ve been omitting initialization for many of our properties in our sample code. Luckily, the framework handles this. When the framework finds a null property in a chain that it needs to navigate, it’ll attempt to create a new instance of the appropriate type and set it onto the property. However, this requires two things on the developer’s part. First, the type of the property must be a class that conforms to the JavaBeans specification, in that it provides a no-argument constructor. Without this, the framework can’t instantiate an object of the type. Next, the property must also conform to the JavaBeans specification by providing a setter method. Without this setter, the framework would have no way of injecting the new object into the property. Keep these two points in mind and you’ll be good to go.
In addition to targeting properties onto which the framework should move incoming data, we also use OGNL when the data leaves the framework. After the request is processed, we use the same OGNL expression to target the same property from a Struts 2 tag. Recall that the domain model data stays on the ValueStack from start to finish. Thus, tags can read from the same location that the interceptors write.
The following snippet shows the property tag doing just this:

<h5>Congratulations! You have created </h5>
<h3>The <s:property value="portfolioName" /> Portfolio</h3>

In this snippet, we see that the Struts 2 property tag takes an OGNL expression as its value attribute. This expression targets the property from which the property tag will pull the data for its rendering process, a simple process where it merely converts the property to a string and writes it into the page. As you can see, OGNL expressions, as commonly used in Struts 2, serve as pointers to properties. Whether the use case is writing to or reading from that property is up to the context. Though not nearly as common, you can also use the fuller features of the OGNL expression language, operators in particular, to write self-contained expressions that, for instance, set the data on a property themselves. But, as this is outside of the normal Struts 2 use case, we’ll only discuss such features in the advanced section.

<--PREVIOUS NEXT--->

No comments: