Skip to content

Instantly share code, notes, and snippets.

@ag88
Created April 16, 2025 08:45
Show Gist options
  • Select an option

  • Save ag88/a0232510c28b4c45b82943527b7ea87e to your computer and use it in GitHub Desktop.

Select an option

Save ag88/a0232510c28b4c45b82943527b7ea87e to your computer and use it in GitHub Desktop.
Apache Wicket reusable Data List

An Apache Wicket reusable Data List

This component displays a list of JavaBeans as a html table

DataListPanel takes as input in the constructor :

  • the wicket:id of the component
  • itemclass The java class of the JavaBean
  • List items the list of JavaBeans
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<body>
<wicket:panel>
<table id="dataitems" class="table table-striped">
<thead class="table-secondary">
<th class="col-md-1" wicket:id="hdrRepeater"></th>
</thead>
<tbody>
<wicket:container wicket:id="dataRepeater"></wicket:container>
</tbody>
</table>
</wicket:panel>
</body>
</html>
package com.mywicket.component;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.util.Enumeration;
import java.util.List;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public class DataListPanel<T extends Serializable> extends Panel {
Logger log = LoggerFactory.getLogger(DataListPanel.class);
List<T> items;
Class<T> itemclass;
public DataListPanel(String id, Class<T> itemclass, List<T> items) {
super(id);
this.items = items;
this.itemclass = itemclass;
init();
}
protected void init() {
Marker marker = MarkerFactory.getMarker("init()");
try {
BeanInfo beaninfo = Introspector.getBeanInfo(itemclass);
PropertyDescriptor[] propdesc = beaninfo.getPropertyDescriptors();
RepeatingView hdrItems = new RepeatingView("hdrRepeater");
for(PropertyDescriptor pd : propdesc) {
log.debug(marker, "{}", pd.getName());
hdrItems.add(new Label(hdrItems.newChildId(), pd.getName()));
}
add(hdrItems);
RepeatingView dataRows = new RepeatingView("dataRepeater");
for(T item : items) {
dataRows.add(new DataRowPanel<T>(dataRows.newChildId(), new Model<T>(item), itemclass));
}
add(dataRows);
} catch (IntrospectionException e) {
log.error(marker, e.getMessage());
}
}
private static final long serialVersionUID = 773465778036471818L;
}
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<body>
<wicket:panel>
<tr>
<td wicket:id="dataField" class="col-md-1"></td>
</tr>
</wicket:panel>
</body>
</html>
package com.mywicket.component;
import java.beans.BeanDescriptor;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.model.IModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Marker;
import org.slf4j.MarkerFactory;
public class DataRowPanel<T> extends Panel {
Logger log = LoggerFactory.getLogger(DataRowPanel.class);
Class<T> itemclass;
public DataRowPanel(String id, IModel<T> model, Class<T> itemclass) {
super(id, model);
this.itemclass = itemclass;
init();
}
protected void init() {
Marker marker = MarkerFactory.getMarker("init()");
T bean = (T) getDefaultModelObject();
try {
BeanInfo beaninfo = Introspector.getBeanInfo(itemclass);
PropertyDescriptor[] propdesc = beaninfo.getPropertyDescriptors();
RepeatingView fields = new RepeatingView("dataField");
for(PropertyDescriptor pd: propdesc) {
try {
Object o = pd.getReadMethod().invoke(bean);
if(o != null)
fields.add(new Label(fields.newChildId(),o.toString()));
else
fields.add(new Label(fields.newChildId(), ""));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
log.error(marker, e.getMessage());
}
}
add(fields);
} catch (IntrospectionException e) {
log.error(marker, e.getMessage());
}
}
private static final long serialVersionUID = 773465778036471818L;
}
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org/dtds.data/wicket-xhtml1.4-strict.dtd">
<head>
<meta charset="utf-8" />
<title>Person List example</title>
</head>
<body>
<h2>Persons</h2>
<p>
<div wicket:id="dataPanel"></div>
</body>
</html>
package com.mywicket.wicket;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.markup.html.WebPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Example extends WebPage {
Logger log = LoggerFactory.getLogger(Example.class);
public class Person implements Serializable {
String firstname;
String lastname;
public Person() {
}
public Person(String firstname, String lastname) {
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
private static final long serialVersionUID = 3754407033439696245L;
}
public Example() {
super();
List<Person> persons = new ArrayList<>(10);
persons.add(new Person("Smith", "John"));
persons.add(new Person("Johnson", "Emily"));
persons.add(new Person("Williams", "David"));
persons.add(new Person("Brown", "Sarah"));
persons.add(new Person("Davis", "Michael"));
DataListPanel<Person> datalist = new DataListPanel<>("dataPanel", Person.class, persons);
add(datalist);
}
private static final long serialVersionUID = 1L;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment