Search from Struts2 Library

Sunday, September 21, 2008

Set tag

In the context of this tag, setting means assigning a property to another name. Various reasons for doing this exist. An obvious use case would be taking a property that needs a deep, complicated OGNL expression to reference it, and reassigning, or setting, it to a top-level name for easier, faster access. This can make your JSPs faster and easier to read. You can also specify the location of the new reference. By default, the property becomes a named object in the ActionContext, alongside the ValueStack, session map, and company. This means you can then reference it as a top-level named object with an OGNL expression such as #myObject. However, you can also specify that the new reference be kept in one of the scoped maps that are kept in the ActionContext. Table below provides the attributes for the set tag




Here’s an example from the chapter 6 sample code:

<s:set name="username" value="user.username"/>
Hello, <s:property value="#username"/>. How are you?

In this case, we aren’t saving much by making a new reference to the username property.However, it illustrates the point. In this sample, the set tag sets the value from the user.username expression to the new reference specified by the name property. Since we don’t specify a scope, this new username reference exists in the default “action” scope—the ActionContext. As you can see, we then reference it with the # operator. Figure below shows the output.

In case you’re wondering what it looks like to set the new reference to a different scope, the following sets the new reference as an entry in the application scope map that is found in the ActionContext:

<s:set name="username" scope="application" value="user.username"/>
Hello, <s:property value="#application['username']"/>. How are you?

Note that we have to use the OGNL map syntax to get at the property in this case. We can’t say we’ve made any readability gains here, but we have managed to persist the data across the lifetime of the application by moving it to this map. It’s probably not a good idea to persist a user’s username to the application scope, but it does serve to demonstrate the tag functionality

Important :
The push tag, and even the set tag to a limited extent, can be powerful when trying to reuse view-layer markup. Imagine you have a JSP template that you’d like to reuse across several JSP result pages. Consider the namespace of the OGNL references in that JSP template. For instance, maybe the template’s tags use OGNL references that assume the existence of a User object exposed as a model object, as in ModelDriven actions. In this case, the template’s tags would omit the user property and refer directly to properties of the user, for example .
If you try to include this template in the rendering of a result whose action exposes a User object as a JavaBeans property, rather than a model object, then this reference would be invalid. It would need to be
.
Luckily, the push tag gives us the ability to push the user object itself to the top of the ValueStack, thus making the top-level references of the template valid in the current action. In general,the push tag and the set tag can be used in this fashion.

No comments: