开发者

Wicket Page with a list of Panels

I'm working on a web application using Apache Wicket and I have three types of page that are basically a numbered list. The difference between each is how the items in the list are displayed. (i.e. one has a header line and a paragraph, another just has the paragraph). Eventually, the data will come from a database, but that is not available at the moment.

I think I can do this by creating a Page that displays a RepeatingView that gets its items from an ArrayList of Panels. I would create a different Panel for each type of list item. Then I could extend the aforementioned Page to three subclasses, one for each specific type of Panels I want. Am I on t开发者_Go百科he right track, or is there a better way to do this?


I find RepeatingView to be very flexible with this sort of thing and you shouldn't require three separate pages. It only expects a component to be added to the repeater, not what kind of component. As long as you keep your Wicket IDs consistent, you can even mix components that come with their own markup (e.g. Panel/Fragment).

I also discourage you from using a List of Panels. It just makes good programming sense for the list to contain your data and then add the appropriate view container based on some flag.

So, markup like this:

<div wicket:id="repeater">
   <div wicket:id="listItem" />
</div>

Works with something like:

RepeatingView rv = new RepeatingView("repeater");

for (DataObject o : dataList) {
   // You can probably add to the rv directly, but this is the common usage
   WebMarkupContainer c = new WebMarkupContainer(rv.newChildId());
   rv.add(c);
   if (shortVersion)
       c.add(new ShortPanel("listItem", new Model<DataObject>(o)));
   else
       c.add(new LongPanel("listItem", new Model<DataObject>(o)));
}

Instead of providing a List of Panels to your page, you provide the same list of Data from the database and then add different panels based on the current view.

In the end, you have one page (with a flag for view type) and different panels (or fragments) for how your data should look according to each view type.


I don't think you'll have to subclass your Page. As far as I can see, this one doesn't change at all. All the changes are within the panels. So basically all you need to do is to provide your repeating-view with different lists of panels. You wouldn't even need to create different classes for these if the differences are as small as presenting a headline or not. Just set the headline to an empty string and set it to invisible...

Basically yes, you're on one of the right tracks, maybe not the one I'd choose but yours will work too...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜