How to dynamic add attribute to a HTML tag in Wicket?
Written on
September 25, 2009 at 1:41 am by
mkyong
Wicket is providing a very handy design to access the HTML tag directly, by inject a Wicket’s id into HTML tag, we have full control of the tag in our wicket java side.
For example, we have a HTML text box component and wrap by a div tag. The div tag should be highlight in error color when text box validation is failed. We can implement this feature easily by using Wicket’s “AbstractBehavior” class, just add this class into our div Wicket’s component as follow :
HTML
<div wicket:id="wicket_id_test">
Hello ~ Wicket leaning curve is high, do you?
</div>Java (Wicket)
WebMarkupContainerWithAssociatedMarkup divtest = new WebMarkupContainerWithAssociatedMarkup("wicket_id_test"); //validation failed , add AbstractBehavior to the divtest container divtest.add(new AbstractBehavior() { public void onComponentTag(Component component, ComponentTag tag) { tag.put("style", "background-color:red"); }}); }
Result
<div wicket:id="wicket_id_test" style="background-color:red">
Hello ~ this is testing for adding attribute into above tag in Wicket ~
</div>Done, we have full control of our HTML tag in Wicket now ~ enjoy

