In Wicket 1.4, I’m trying to allow child pages to alter a CSS class on a tag in the parent page, which I do all the time. What is odd about this case is that the tag I want to target wraps the child page markup. Here’s a simplified snip of what I tried:
ParentPage.html
<div id="main" wicket:id="main">
<wicket:child />
</div>
ParentPage.java
public abstract class ParentPage {
private WebMarkupContainer main;
protected ParentPage() {
main = new WebMarkupContainer("main");
add(main);
}
public void setClassAttr(String cssClass){
main.add(SimpleAttributeModifier("class", cssClass);
}
}
ChildPage.html
<wicket:extend>
...
</wicket:extend>
ChildPage.java
public class ChildPage extends Page {
...
public ChildPage() {
super();
...
setClassAttr("specific-class-for-this-page");
}
}
…Which blows up because it appears the HTML from the child loads, but not the java. (If I remove the wicket:id and java code on div#main, all is well.)
Note that the tag on the parent that I want to manipulate from the child is actually wrapping the wicket:child tag. In other cases I have done something similar, the tags I want to monkey with tend to be siblings or otherwise distant to the wicket:child tag.
All I really want to do is allow the child to change the class attribute on the parent – is there another way to do this? Why can’t a child page be nested under another Wicket page component?
First of all, it has nothing to do with actually setting the attribute, but with putting
<wicket:child>inside a container.Now imagine if
ChildPagewas aPanel, what would the code of yourParentPagelook like? It would contain a line somewhere sayingmain.add( new ChildPanel() ). That’s how themaincomponent knows that when it renders, it should call the rendering method of your child panel too.But with inheritance it’s different. Your
maincomponent has no way of knowing what<wicket:child>should resolve to. Marking yourmaincontainer transparent tells Wicket to ask the parent component (that is, your page component) to resolve and render it.