Jakarta > Tapestry
Jakarta
 
Font size:      

Property Injection

Tapestry 4.0 introduces an entirely new concept into Tapestry application development: property injection. By use of the <inject> element in page and component specifications, it is possible to add new properties to pages or components.

The new properties that are created often are more than just wrappers around an instance variable; in many cases they create complex synthetic accessor methods.

There are four types of property injection built into Tapestry. These types correspond to the type attribute of the <inject> element:

object
Injects objects defined by the HiveMind Registry.
state
Creates a property providing access to an application state object.
meta
Creates properties that allow access to component meta-data properties, and can automatically convert values from strings to other types.
script
Creates a property containing a parsed IScript, ready to execute.

Like so much in Tapestry, this list is open to extension. The tapestry.enhance.InjectWorkers configuration point defines new types, and the HiveMind services that implement those types.

meta injection

The meta injection type provides a page or component with access to its meta data. Meta data for a component is primarily provided as <meta> tags in the component or page specification.

However, meta-data may be specified elsewhere; the search starts in the component (or page) specification, but if not defined there, the search continues inside the component's namespace (its application or library specification). If no value is found there, the search continues in the list of application property sources. In other words, there are multiple places to set defaults, which can be overridden.

Beyond wide searching, the other added value for the meta property injection is type coercion. Meta data always starts as simple strings, but your properties may be of any type. Tapestry will attempt to coerce the string value to your desired type.

For example, perhaps you want to use meta-data to control the number of items from some large list that is displayed on a single page. You can define the meta-data, and the property in your page or component specification:


  <meta key="page.size" value="15"/>
  
  <inject property="pageSize" type="meta" object="page.size"/>

You can access the this meta data value in code by defining a property:

  
  public abstract int getPageSize();
  

script injection

Tapestry includes extensive support for creating client-side JavaScript. At the core of this are specialized script templates. These templates must be parsed into IScript objects in order to be used. The script injection type hides the details of this process, and simply represents the parsed script template as a read-only property.

The object is the relative path to the script template; it is evaluated relative to the page or component specification (typically, it is another file in the same folder).

This example is from the Palette component:

  <inject property="script" type="script" object="Palette.script"/>

The script can then be executed from the Java code:

  public abstract IScript getScript();

  . . .   
  
  PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, this);
  
  getScript().execute(cycle, pageRenderSupport, _symbols);