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

Hello ~ Wicket leaning curve is high, do you?

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 :

Hello ~ this is testing for adding attribute into above tag in Wicket ~

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

avatar
4 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
5 Comment authors
Shifa SalheenVinod Kumar KashyapJuissiSujitDesign Template Recent comment authors
newest oldest most voted
Design Template
Guest
Design Template

I believe that is one of the most significant information for me. And i’m happy studying your article. However wanna statement on few general issues, The website taste is great, the articles is in reality nice : D. Good activity, cheers

Sujit
Guest
Sujit

Hi mkyong,

The AbstractBehavior class has been deprecated in wicket 6.0 version, so how can we achieve above functionality without it or is there any behavior to add HTML tag dynamically?

Vinod Kumar Kashyap
Guest
Vinod Kumar Kashyap

Hi Mkyong,

Works like Magic.
Thanks once again.

Shifa Salheen
Guest
Shifa Salheen

Hi Mkyong .Your tutorials are really very informative and cleanly coded.The simplicity of the code makes it so easy to understand. Thanks