SpiderRico
SpiderRico

Reputation: 2046

Using wicket CompoundPropertyModel with abstract classes

I've the following abstract class:

public abstract class Person extends Model {
private Serializable id;
private String name;
private Integer year;
private String nationality;
private String deathYear;

@Override
protected Serializable getId() {
    return id;
}

@Override
protected Model byId() {
    return null;
}

public String getName() {
    return name;
}

public abstract void add();

public abstract void update();

public abstract void delete();

public abstract String getDeathYear();

public abstract int getYear();

public abstract String getNationality();

public abstract List<Book> getBooks();

}

And the following class which extends it:

public class Author extends Person {

private Serializable id;
private String name;
private Integer year;
private String nationality;
private String deathYear;
private List<Prize> prizes;
private List<Book> books;

public Author() {

}

@Override
public String getNationality() {
    return nationality;
}

@Override
public String getName() {
    return name;
}

@Override
public int getYear() {
    return year;
}

@Override
public String getDeathYear() {
    return deathYear;
}

@Override
public List<Book> getBooks() {
    return books;
}

public List<Prize> getPrizes() {
    return prizes;
}

public void setName(String name) {
    this.name = name;
}//plus some unrelated stuff

I'm trying to create a form like this:

public class AdminAuthorPage extends AdminBasePage {
Author inputAuthor;

public AdminAuthorPage() {
    this(new Author());
}

public AdminAuthorPage(Author author) {
    this.inputAuthor = author;

    this.add(new ListView<Author>("authorList", Author.all()) {
        @Override
        protected void populateItem(ListItem<Author> item) {
            final Author author = item.getModelObject();
            item.add(new PersonLink("authorLink", author));
            item.add(new Link("editLink") {
                @Override
                public void onClick() {
                    setResponsePage(new AdminAuthorPage(author));
                }
            });
            item.add(new Link("deleteLink") {
                @Override
                public void onClick() {
                    author.delete();
                    setResponsePage(new AdminAuthorPage());
                }
            });
        }

    });

    Form authorForm = new Form("authorForm", new CompoundPropertyModel(
            this.inputAuthor));
    authorForm.add(new RequiredTextField<String>("name"));
    //....

However, when I browse into the corresponding page I get the following error:

 Last cause: null
 WicketMessage: 
 Error attaching this container for rendering: [Form
 [Component id = authorForm]]

As far as I know, this error occurs when the type of the CompoundPropertyModel(Author in this case) couldn't find the required getters.

However, it seems that my code has defined getter method for the name attribute.

Upvotes: 0

Views: 214

Answers (1)

Roman Grigoriadi
Roman Grigoriadi

Reputation: 1928

I think you misunderstood concept of Models. Your person class should not extend model, it should be standard pojo. You than declare model to be generic to your pojo.

MyPage extends GenericWebPage<Author> {

  public MyPage(IModel<Author> model) {
    super(model);
  }

}

Upvotes: 2

Related Questions