Search code examples
modelwicketwicket-6wicket-1.6

Wicket 6: Nested object's property without IModel hustle


I have nested models like:

class User {
  private String name;
  private Address address;
  ...
}

class Address {
  private String city;
  ...
}

now, in Wicket 6, can I have a singe IModel to access all nested properties like:

IModel<User> userModel = new PropertyModel<>(user);
Form<User> form = new CSRFSafeForm<>("form", user);
form.add(new TextField<>("name"));
form.add(new TextField<>("address.city"));

Is it possible without any additional coding?

I've read Wicket's manual https://cwiki.apache.org/confluence/display/WICKET/Working+with+Wicket+models but it says I need to create a new form and IModel.

Is it possible to edit both name and city on the same form?


Solution

  • I don't know where CSRFSafeForm is coming from.

    But you can do the same with a standard Form and CompoundPropertyModel:

    IModel<User> userModel = new PropertyModel<>(user);
    Form<User> form = new Form<>("form", new CompoundPropertyModel<User>(user));
    form.add(new TextField<>("name"));
    form.add(new TextField<>("address.city"));