Get the value of a component tag attribute

In the wicket, I need to get the value of the component's markup tag, for example:

<a href="#" name="#">...</a>

      

Here I need the value of an attribute name

in the Wicket java class, for example:

String name = /*{link tag name}*/;

      

I need to assign a value name

as an attribute of a name

tag <a>

.

+3


source to share


1 answer


As I understood from your question, you need to dynamically set / set the tag parameters of your gate components.

So, you have several options for this:

  • Use static methods AttributeModifier

    such as #append("class", "appendedClass")

    or #replace("name", "#")

    (where the first parameter is a tag attribute and the second is a value) to set whatever value you want to use, but note that t get the current tag values ​​with this approach. Also note that you do not add these modifiers to "render" methods (for example #onConfigure()

    ), and do not create duplicate modifiers on every page or update the component in the browser.

  • Cancel the method #onComponentTag()

    like this.

    ...new Link ( "link" ) {
        @Override
        protected void onComponentTag ( final ComponentTag tag )
        {
            super.onComponentTag ( tag ); // you should always call super.

            tag.getName (); // get name of the tag: a/div/span..
            tag.setName ( "span" ); // set tag name.
            tag.getAttribute ( "name" ); // get 'name' attribute value.
            tag.put ( "name", "#" ); // set 'name' attribute value
        }
    }        

      



  1. Use the same approach as above, but usually the method Behavior

    onComponentTag

    does not override component 1.

Read this one for more information, but note that this is a fairly old article and may be different for newer versions of Wicket. (for example, no longer need to be used AttributeAppender

as it moves into methods of the AttributeModifier class).

Also, you can see this or these examples. There are a lot of them on google.

+4


source







All Articles