There are three different approaches to adding or updating a Components HTML attributes.
The first approach is the simplest approach which overrides the Components "onComponentTag" method. In your WebPage:
You can also add to existing tag attributes using the onComponentTag:
A decoupled approach is to use a generic AbstractBehavior. In your WebPage:
Another decoupled approach is to use an AttributeModifier/AttributeAppender.
To modify a tag attribute, 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:
In your HTML you would have something like this:
And in your WebPage Java something like this:
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.
Another example would be to use the AttributeAppender:
Use IMarkupFilter if you are using markup that is not attached to any component