Rory
Rory

Reputation: 35

Creating a list of links with a loop in wicket

I am new to Wicket, so I'm not sure if this is a stupid question or not, either way, I couldn't find an answer by googling a lot and searching on stackoverflow.

I'm trying to create an html list of links generated by the contents of an arraylist. This list may vary in size, so I can't create static links, they have to be generated by a loop. The list is on my HomePage.html and I want all these links to refer tot OtherPage.html, but with different parameters. The link should have the same name as the parameter "name" I'm passing through. My 'try' is as following: (I'm using generic names here to keep things simple)

RepeatingView list = new RepeatingView("list");
for (final String s : names) {
 Link l = new Link("link") {
  @Override
  public void onClick() {
   PageParameters pageParameters = new PageParameters();
   pageParameters.add("name", s);
   setResponsePage(OtherPage.class, pageParameters);
  }
 };
 l.setBody(Model.of(s));
 list.add(new Label(list.newChildId(), l));
}
add(list);

This the html part that I thought would be needed:

<ul>
 <li wicket:id="list"></li>
</ul>

I did another attempt using ListView with no success

List<String> namesList = Arrays.asList();
for (String s : names) {
    namesList.add(s);
}
add(new ListView<String>("names", namesList) {
    @Override
    protected void populateItem(final ListItem<String> item) {
        Link l = new Link("name") {
            @Override
            public void onClick() {
                PageParameters pageParameters = new PageParameters();
                pageParameters.add("name", item.getModel());
                setResponsePage(PatternPage.class, pageParameters);
            }
        };
        l.setBody(Model.of(item.getModel()));
        item.add(l);
    }
});

with the following html:

    <div id="bd" style="display: table;">
        <div wicket:id="names" style="display: table-row;">
            <div style="display: table-cell;"></div>
            <div wicket:id="name" style="display: table-cell;"></div> </div>
    </div>

it resulted in this error:

Last cause: null
WicketMessage: Can't instantiate page using constructor 'public paf.assignment3.HomePage(org.apache.wicket.request.mapper.parameter.PageParameters)' and argument ''. An exception has been thrown during construction!

Upvotes: 0

Views: 1467

Answers (2)

Rory
Rory

Reputation: 35

I got it working. I had to add the link to the list and leave out the labels. Here's the java code. First I used a loop for adding all links.

    RepeatingView list = new RepeatingView("list");
    for (final String s : names) {
        list.add(createLink(list.newChildId(), s));
    }
    add(list);

I made a separate method for creating the links itself, to keep a clean code. Here's the method createLink:

    public Link createLink(String linkName, final String name) {
        Link l = new Link(linkName) {
            @Override
            public void onClick() {
            PageParameters pageParameters = new PageParameters();
            pageParameters.add("name", name);
            setResponsePage(PatternPage.class, pageParameters);
        }

    };
    l.setBody(Model.of(name));
    return l;
}

The html stayed like this:

    <ul>
        <li wicket:id="list"></li>
    </ul>

Upvotes: 2

svenmeier
svenmeier

Reputation: 5681

If you're using setResponsePage(OtherPage.class, pageParameters) you page class has to have a constructor with PagaParameter argument.

Or just use setResponsePage(new OtherPage(name)) instead.

Upvotes: 0

Related Questions