Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Code Block
Image img = new Image("my-img-id") {
	@Override
	protected void onComponentTag(ComponentTag tag) {
		// illustrates how to prevent an image from caching by adding a random value to the src attribute
		// This is similar to the code thats in NonCachingImage and would be the preferable solution for this
		super.onComponentTag(tag);
		String src = (String) tag.getAttributes().get("src");
		src = src + "&rand=" + Math.random();
		tag.getAttributes().put("src", src);
	}
};

...

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:

Code Block
...

new TextField("my-text-field", myModel).add(new AttributeAppender("onmouseover", new Model("foo();return false;"), ";"));

...
Tip
titleRaw Markup

Use IMarkupFilter if you are using markup that is not attached to any component