WicketのHTMLタグに属性を動的に追加する

WicketでHTMLタグに動的に属性を追加する

Wicketでは、HTMLタグに簡単にアクセスまたは操作できます。 たとえば、HTMLテキストボックスコンポーネントがあり、divタグでラップされている場合、テキストボックスの検証に失敗した場合は、divタグをエラー色で強調表示する必要があります。

上記の場合、「AbstractBehavior」クラスを実装して、HTMLタグに属性を動的に追加できます。 次の例を参照してください。

元のHTML

    Hello ~ Wicket leaning curve is high, do you?

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

このような結果:

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