Snekse
Snekse

Reputation: 15799

Dynamic HTML attributes in Wicket

I know there are several ways to do this, but I'm looking for the best way (I'm new to Wicket).

In the HTML code below, I've marked 2 areas where I need to replace some dummy text. The text in question is the {APP_TITLE} and {PAGE_TITLE}. I just want to replace them with simple Strings.

QUESTION

In the first <div>, what is the best way to replace those values without loosing the contents within the DIV and the attributes that currently define the div (and whatever attributes are added in the future)?

<html lang="en">
<head>
    <!-- Replace here -->
    <title>{APP_TITLE} - {PAGE_TITLE}</title> 
</head>
<body>
    <!-- Replace here -->
    <div class="corpAppTemplate" corp:app="{APP_TITLE}" corp:title="{PAGE_TITLE}">
        <ul class="corpAppMenu"  wicket:id="MenuList">
        </ul>
        <span wicket:id="UserID">UserID</span>
               <wicket:child/>
    </div>
</body>
</html>

Upvotes: 2

Views: 5175

Answers (1)

stevemac
stevemac

Reputation: 2554

You can use the AttributeModifier to change specific attributes on any given tag, you will just need to add a wicket:id to the tag to enable wicket to detect where you want the change.

eg. In the HTML replace:

<div class="corpAppTemplate" corp:app="{APP_TITLE}" corp:title="{PAGE_TITLE}">

with

<div wicket:id="template" class="corpAppTemplate" corp:app="{APP_TITLE}" corp:title="{PAGE_TITLE}">

And add the following to your java page:

template = new WebMarkupContainer("template");
add(template);

template.add(new AttributeModifier("corp:app", true, new Model("{APP_TITLE}")));
template.add(new AttributeModifier("corp:title", true, new Model("{PAGE_TITLE}")));

For more details on AttributeModifier, see the wicket documentation http://wicket.apache.org/apidocs/1.4/org/apache/wicket/AttributeModifier.html

Upvotes: 7

Related Questions