开发者

How is Wicket's DataView used?

I have a Wicket page, and I need to print my records on the开发者_运维百科 screen. I keep the records in my database. How can I do it with DataView? I need code samples.


There's an ancient example on this page:

https://cwiki.apache.org/WICKET/a-simple-dataview-example.html

It's still mostly valid, the only thing that has changed is that the current versions of wicket support generics. So here's the updated source of the code from that page:

Java Code:

public class Sub1Page extends WebPage{

    private static final long serialVersionUID = 1L;

    public Sub1Page(){

        Contact contact = null;
        final List<Contact> list = new ArrayList<Contact>();

        char character;

        // a - z
        for(int i = 97; i < 123; i++){
            character = (char) i;
            contact = new Contact(String.valueOf(character));
            list.add(contact);
        }

        final DataView<Contact> dataView =
            new DataView<Contact>("simple", new ListDataProvider<Contact>(list)){

                private static final long serialVersionUID = 1L;

                @Override
                public void populateItem(final Item<Contact> item){
                    final Contact user = item.getModelObject();
                    item.add(new Label("id", user.getId()));
                }
            };

        dataView.setItemsPerPage(10);

        add(dataView);

        add(new PagingNavigator("navigator", dataView));
    }

}

HTML Code (unchanged):

<wicket:extend>

<table cellspacing="0" class="dataview">
    <tbody>
       <tr wicket:id="simple">
         <td><span wicket:id="id">Test ID</span></td>
       </tr>
    </tbody>
</table>

<div wicket:id="navigator"></div>

</wicket:extend>


DataView is a basic implementation of AbstractDataView. Data views aim to make it very simple to populate your repeating view from a database by utilizing IDataProvider to act as an interface between the database and the dataview.

Example:

 <tbody>
   <tr wicket:id="rows">
     <td><span wicket:id="id">Test ID</span></td>
     ...

Though this example is about a HTML table, DataView is not at all limited to HTML tables. Any kind of list can be rendered using DataView.

And the related Java code:

add(new DataView<UserDetails>("rows", dataProvider)
 {
    public void populateItem(final Item<UserDetails> item)
    {
            final UserDetails user = item.getModelObject();
            item.add(new Label("id", user.getId()));
    }
 });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜