Versions Compared

Key

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

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:

Code Block

...

// this can be any Component
new TextField("my-text-field", myModel){

	@Override
	protected void onComponentTag(final ComponentTag tag){
		super.onComponentTag(tag);
		tag.put("onmouseover", "foo();return false;");
	}
};

...

A decoupled approach is to use a generic AbstractBehavior. In your WebPage:

Code Block

...

new TextField("my-text-field", myModel).add(new AbstractBehavior(){

	@Override
	public void onComponentTag(Component component, ComponentTag tag) {
		tag.put("onmouseover", "foo();return false;");
	}
});

...

Another decoupled approach is to use an AttributeModifier/AttributeAppender.

To modify a tag attribute, there is a nifty new class called wicket. AttributeModifier. Just add an instance of AttributeModifier to the tag's Java component.

When creating the AttributeModifier class the name of the attribue 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:

Code Block
html
html
 <span wicket:id="alarm" class="unknown"/>Alarm Status</span>

And in your java WebPage Java something like this:

Code Block
 Label alarmLabel = new Label("alarm", new PropertyModel(test,  "alarmState"));
 alarmLabel.add(new AttributeModifier("class", new Model() {
   public Object getObject(final Component component) {
         String cssClass;
         if ( test.getAlarmState() ) 
             cssClass = "alarm";
         } else {
             cssClass="noAlarm";
         }
         return cssClass;
   }
 }));
 add(alarmLabel);

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

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;"), ";"));

...