You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

The tag syntax in WebWork is extremely easy to understand. To quickly get started, all you need to know is that all attributes are applied as Strings initially. They are then parsed for the syntax %{ ... }, and anything in between the braces is evaluated against the value stack.

Upgrade note!

The tag syntax was not always this easy – if you are upgrading from WebWork 2.1.7 or previous versions, you may wish to read about the Alt Syntax.

Like most things in life, it turns out that this isn't quite that simple. Specifically, there are actually three rules to be aware of:

  1. All String attribute types are parsed for the %{ ... } characters.
  2. All non-String attribute types are not parsed, but instead evaluated directly as an OGNL expression
  3. The exception to rule #2 is that if the non-String attribute starts with %{ and ends with }, those characters are cut off before evaluating the expression.

The best way to understand these rules is by looking at some examples.

We recognize that these rules can be confusing. Generally, you should not need to know them at all, as 99.9% of the time everything will "just work". However, as we see in the examples, there are some tricky situations that require understanding of these rules. In future versions of WebWork, will be trying to make the tag syntax even simpler

Some Examples

The most basic example explaining how the tag syntax works is as follows. This example shows off rule #1 only:

<ww:textfield label="%{getText("state.label")}" name="state"/>

In this example, the label is dynamically evaluated and set to the outcome of the OGNL expression getText("state.label"), which will in turn invoke the Internationalization system are retrieve the value of the i18n key state.label. The name, being a String attribute, is simply set to the string state.

The next example shows off rule #2:

<ww:select label="%{getText("state.label")}" name="state" multiple="true"/>

While this looks very similar to the last example, the key thing to recognize is that the multiple attribute is of type Boolean, which means it falls under rule #2. Generally you won't even notice this, because true as an OGNL expression evaluated to true, which is what you want.

Now let's suppose we want to extend this example to show off rule #3 by making the multiple attribute dynamic:

<ww:select label="%{getText("state.label")}" name="state" multiple="%{allowMultiple}"/>

Because the attribute is of type Boolean and starts and ends with the correct characters from rule #3, it is reduced to the expression allowMultiple, which is evaluated against the value stack, returning a true of false value, just like in the previous example.

There is one trick example to keep an eye on, however. For example, the following is probably incorrect:

<ww:textfield label="%{getText("state.label")}" name="state" value="CA"/>

This example will only work if the expression CA can result in something, meaning that your action has a method getCA(), which is probably not what you expected. This is because the value attribute is of type Object and therefore rule #2 applies. If the desire is to set a static String as the initial value, you would need to supply an OGNL expression that returns a String. For example, this is the correct way to do it:

<ww:textfield label="%{getText("state.label")}" name="state" value="%{'CA'}"/>

While you could set the value attribute as just "'CA'", we recommend the parsed expressions so that, in the future when WebWork supports parsed attributes for all types, your code will still work.

  • No labels