Actions and bean subclases

0 views
Skip to first unread message

Fernando Boucquez

unread,
Apr 28, 2009, 10:16:45 AM4/28/09
to wicket-w...@googlegroups.com
Hello, in the followed scenario the BeanActionButton throws an exception when i click in an action:

A simple dog serializable:

public class Dog implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;

    private int age;

    public Dog(final String name, final int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public Dog() {
        super();
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public void setAge(final int age) {
        this.age = age;
    }

    public String bark() {
        return "wuaw..";
    }

}

a subclass of the dog, Doberman

public class Doberman extends Dog {

    private static final long serialVersionUID = 1L;

    public Doberman() {
        super();
    }

    public Doberman(final String name, final int age) {
        super(name, age);
    }

    @Override
    public String bark() {
        return "WAUW, WAUW!!!! (very noise ;))";
    }

}

and in the panel:
 
  ...Constuctor..
  Dog dog = createADog();
  BeanMetaData meta = new BeanMetaData(Dog.class, null, this, null, false);
  add(new BeanForm("wicketBean", dog, meta));


  public void bark(final AjaxRequestTarget target, final Form form, final Dog dog) {
        warn(dog.bark());
    }

    private Dog createADog() {
        return new Doberman("Pedro", 8);
    }

The exception is:

java.lang.RuntimeException: Action method bark(AjaxRequestTarget, Form, com.idtech.g2s.host.test.wicketbean.Doberman/Object) is not defined in class com.idtech.g2s.host.test.wicketbean.WicketBeanTestPanel
at com.googlecode.wicketwebbeans.actions.BeanActionButton.onAction(BeanActionButton.java:89)
at com.googlecode.wicketwebbeans.actions.BeanSubmitButton$2.onSubmit(BeanSubmitButton.java:155)
at org.apache.wicket.markup.html.form.Form.delegateSubmit(Form.java:1471)
at com.googlecode.wicketwebbeans.containers.BeanForm$1.delegateSubmit(BeanForm.java)
at org.apache.wicket.markup.html.form.Form.process(Form.java:911)
When the BeanMetaData is created, adds a correct action bark for the dog

But, when the action is called the real object is a Doberman and the reflection tries to call the action with a Doberman parameter:

Class BeanActionButton, method: onAction
....
       method = component.getClass().getMethod(methodName, new Class[] { AjaxRequestTarget.class, Form.class, bean.getClass() /*Doberman.class*/} );
 ....
I fix the issues changing the previous line for the next one:

method = component.getClass().getMethod(methodName, new Class[] { AjaxRequestTarget.class, Form.class, element.getBeanMetaData().getBeanClass()/*Dog class*/} );

I think the method in charge to call the action does not the same as the method which records the action.

Something similar happen when the class is a interfaces, and the real object is an implementation.

Regards,
Fernando



Dan Syrstad

unread,
Apr 28, 2009, 11:36:31 AM4/28/09
to wicket-w...@googlegroups.com
In your panel, change:


  public void bark(final AjaxRequestTarget target, final Form form, final Dog dog) {
        warn(dog.bark());
    }

to:

  public void bark(final AjaxRequestTarget target, final Form form, final Object dog) {
        warn(dog.bark());
    }

WWB doesn't support polymorphism on the third parameter.

-Dan
Reply all
Reply to author
Forward
0 new messages