Add attribute to a HTML tag dynamically in Wicket
In Wicket, you can access or manipulate the HTML tag easily. Let say, you have a HTML text box component and wrap by a div tag, and the div tag should be highlighted in error color if textbox validation is failed.
In above case, you can implement “AbstractBehavior” class, to add attribute to a HTML tag dynamically. See following example,
Original HTML
<div wicket:id="wicket_id_test">
Hello ~ Wicket leaning curve is high, do you?
</div>Modify with Wicket AbstractBehavior
WebMarkupContainerWithAssociatedMarkup divtest = new WebMarkupContainerWithAssociatedMarkup("wicket_id_test"); //validation failed , add AbstractBehavior to the div test container divtest.add(new AbstractBehavior() { public void onComponentTag(Component component, ComponentTag tag) { tag.put("style", "background-color:red"); } });
Result like this :
<div wicket:id="wicket_id_test" style="background-color:red">
Hello ~ this is testing for adding attribute into above tag in Wicket ~
</div>