Tapestry/OGNL: Could not find an adaptor for class XYZ

Getting the “Could not find an adaptor for class Foo” Mesage in Tapestry? You probably forgot to make your class Serializable. A simple description of a possible problematic situation follows: 

Suppose you have some simple class, like so:

public class MyItem {
    private String itemname;

  public String getItemname() {
       return itemname;
  }
}

And somewhere in your Tapestry page or component, you iterate a list of these items to produce directlinks for each of them:

<span jwcid="@Foreach" source="ognl:myItems" value="ognl:loopItem">
  <a jwcid="@DirectLink" listener="ognl:listeners.myItemListener"
        parameters="ognl:new java.lang.Object[] {loopItem}">
      <span jwcid="@Insert" value="ognl:loopItem.itemname"/>
  </a>
</span>

In your page definition, you have created a nice listener method which you expect to be called:

public void myItemListener(IRequestCycle cycle){
  MyItem item = (MyItem)cycle.getServiceParameters()[0];

  System.out.println(
    "Hi, you have pressed the link for "
    + item.getItemname());
}

You start everything up, but when loading the page, Tapestry complains:

org.apache.tapestry.ApplicationRuntimeException:
  Could not find an adaptor for class MyItem.

… and a horrific stacktrace follows, which you can’t read or understand because it is all about AbstractComponent.renderbody and other internals of Tapestry and your webserver. Although the error is terrible, and does not point you anywhere, the fix is rather simple. You forgot to make your class Serializable! Changing it to :

public class MyItem implement Serializable {
  private String itemname;

  public String getItemname() {
    return itemname;
  }
}

fixes the problem, because now Tapestry can serialize your object into the session, and pass it as a parameter to the listener.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s