Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

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:

Code Block
xml
xml

<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:

Code Block
xml
xml

<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 a bit more complicated, probably incorrect:

Code Block
xml
xml

<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:

Code Block
xml
xml

<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.