Binding a Map to a DropDownChoice in Wicket App



One day i had to change a page (WebPage) in a way that it would allow me to persist a simple attribute of an entity. First i tried to use an Enum, however i’m using JPA persistence with Hibernate and was a little bit concerned about how Hibernate would deal with this Enum in searches or some other more advanced stuff. So i decided to create a simple Integer attribute and set a constant on it.

This is a sample of code using a Map to exhibit a list of options (DropDownChoice) in a Wicket application to persist a constant (Integer) in an entity.

Code of ControlSerum entity where the attribute status will be persisted:

public class ControlSerum {
  private Integer status;

  public final static int STATUS_PREP = 0;
  public final static int STATUS_ACTIVE = 1;
  public final static int STATUS_INACTIVE = 2;
  private final static Map<Integer, String> statusMap = new HashMap<Integer, String>();

    statusMap.put(STATUS_PREP, "Em preparo");
    statusMap.put(STATUS_ACTIVE, "Ativo");
    statusMap.put(STATUS_INACTIVE, "Inativo");

  public Integer getStatus() {
    return status;
  public void setStatus(Integer status) {
    this.status = status;

  public static Map<Integer, String> getStatusMap(){
    return new HashMap<Integer, String>(statusMap);

At ControlSerumEditPage page class, i implemented a IChoiceRenderer to renderize correctly the options of select tag using the Map key as the value of option tag and the Map value as the name for exibition of option tag.

ControlSerumEditPage class constructor snippet where a renderizer for DropDownChoice is instantiated and added to Form:

IChoiceRenderer choicesRenderer = new IChoiceRenderer() {
  public String getDisplayValue(Integer object) {
    return ControlSerum.getStatusMap().get(object);
  public String getIdValue(Integer object, int index) {
    return object.toString();

IModel optionsModel = new Model(){
  public Serializable getObject() {
    return new ArrayList(ControlSerum.getStatusMap().keySet());

IModel statusModel = new PropertyModel<Integer>(controlSerum, "status");

DropDownChoice<Integer> status = new DropDownChoice<Integer>("status", statusModel, optionsModel, choicesRenderer);

To download the full code of this application (still in development):

Please post your doubts or comments and i’ll promptly answer you!


6 comentários sobre “Binding a Map to a DropDownChoice in Wicket App

  1. David

    I think I get it… and I am definitely slow…

    So Cassio, is the idea that instead of using a raw enum (which can’t be mapped with Hibernate since enums aren’t classes), you’re creating a class that wraps your enum so it can be mapped by Hibernate, correct?

    • The Integer is being directly persisted in ControlSerum class. Hibernate does persist Enums, but to keep it simple, I didn’t use it. The objective of this code sample is just to show how to use a DropDownChoice to persist a Integer directly into an Entity.
      Please, be confortable to ask anything! =D

  2. Dmitry

    Hi, Cassio

    Thank you very much for that great example, especially for carefully naming of variables.

    statusModel and optionsModel names just save my day and give me more than I could dig out of plenty of tutorials on DropDownChoice for understanding and implementing DDC for my own needs.

Deixe um comentário

Preencha os seus dados abaixo ou clique em um ícone para log in:

Logotipo do

Você está comentando utilizando sua conta Sair /  Alterar )

Foto do Google+

Você está comentando utilizando sua conta Google+. Sair /  Alterar )

Imagem do Twitter

Você está comentando utilizando sua conta Twitter. Sair /  Alterar )

Foto do Facebook

Você está comentando utilizando sua conta Facebook. Sair /  Alterar )


Conectando a %s