Versions Compared

Key

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

...

To modify a tag attribute, there is a nifty new class called one can use a AttributeModifier. Just add an instance of AttributeModifier to the tag's Java component. When creating the AttributeModifier class the name of the attribute you want to modified must be passed, along with a model containing the value to use. For example:

...

If the attribute is not already present it will not, by default, be created. To quote the Javadoc: "If an attribute is not in the markup, this modifier will add an attribute to the tag only if addAttributeIfNotPresent is true and the replacement value is not null." (see constructor AttributeModifier for more details).

There is a SimpleAttributeModifier which always adds/replaces the attribute, but like the name suggests it is pretty simple and requires the attribute value upfront. If your attribute value is changing all the time you need a modifier that accepts a model, like AttributeModifier.

Code Block

// adds or replaces the class attribute on a component with my-css-class
component.add(new SimpleAttributeModifier("class", "my-css-class"));

Another example would be to use the AttributeAppender:

...