简体   繁体   中英

Wicket Nested Fragment

I'm trying to nest one fragment into one another. A listview is filled with the parent fragment (which works), now I want for some case that a second fragment is nested within the first. Here is my code to populate my listview:

@Override
protected void populateItem(final Item<Message> item) {
    Message msg = item.getModelObject();
    MessageFragment msgFragment = new MessageFragment("entry", "messageFragment", this, msg);
    WebMarkupContainer msgRatingContainer = new WebMarkupContainer("messageRatingContainer");
    msgRatingContainer.setVisible(false);
    if(msg.getType() == MessageType.RESERVATION){
        msgRatingContainer.add(new MessageRatingFragment("messageRatingContainer", "messageRatingFragment",this, msg));                 
        msgRatingContainer.setVisible(true);
    }
    msgFragment.add(msgRatingContainer);
    item.add(msgFragment);
}

The involved markup:

<li wicket:id="listView"><div wicket:id="entry"></div></li>
<wicket:fragment wicket:id="messageFragment">
    <div class="MessageAlert">
        My Parent fragment content
    </div>
    <div  wicket:id="messageRatingContainer"></div>
    <wicket:fragment wicket:id="messageRatingFragment">
        My nested fragment content
    </wicket:fragment>
</wicket:fragment>

If I don't have any nested fragment (the if condition is false) the UI is presented as expected, if the condition is true I receive following error:

org.apache.wicket.WicketRuntimeException: The component(s) below failed to render. A common problem is that you have added a component in code but forgot to reference it in the markup (thus the component will never be rendered).
1. [MarkupContainer [Component id = messageRatingContainer]]
2. [MarkupContainer [Component id = messageRatingContainer]]

I suppose is something connected with the component hierarchy but I can't find out which way is the right one.

The problem seems to be that if msg.getType() != MessageType.RESERVATION , you don't add anything under the id messageRatingContainer .

You should always add it, then call setVisible() with true and false depending on whether you want it to be rendered.

By the way, from the code snippet above I can't really see the need for the use of fragments at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM