Beliebte Suchanfragen
|
//

Struts2 Interface and EJB issues

29.10.2008 | 2 minutes of reading time

Today I want to write about an issue that occurred to me with Struts2, but it is also valid for other scenarios. Ill first give an example:

Elections in the States are coming close, so we are going to make a favorite poll.

First implement a service that has three methods:

1public interface ElectionFavorite {
2  public List getFavs(int userId);
3  public List getChoices();
4  public void setFavs(int userId, List);
5}

Looks pretty straightforward. Our choices are a list of top candidates, including Homer Simpson, Dilbert and Captain Kirk for possible presidents.

To make our poll we write a small Struts2 based application and use the CheckboxList to display the choices. We use the two getters from the an EJB implementation of this interface to obtain the possible choices and the made selections in case the guest is changing its vote.

The application works fine, we make our choice and save.

1NoClassDefFoundException: com.opensymphony.xwork.util.XWorkList not found

How come? Well Struts2 does some conversion magic. It converts the returned selections from the page, which are HTML arrays, into this type, for whatever reason they think it is appropriate. But when we passing this List instance to the EJB that List implementation is most likely not on the classpath. Depending on your classloading setup when trying this, it might work, but when the EAR the EJB is in has no access to struts/xwork jars from the Web Application you will be getting this error.

To fix this there are some options on the client side. One might be to implement a proxy that converts the List:

1setFavs(List favs){
2  this.favs = new ArrayList();
3  this.favs.addAll(favs);
4}

And now the general learning from this exercise: Using interfaces in APIs is great, because it allows the usage of different implementations. But when the chosen implementation is not found on the receiver’s classpath bad things can happen. There are three options to resolve this:

  1. Make sure that all client classes are available on the service classpath
  2. Make a project convention that only JDK implementations are passed
  3. Don’t use Interfaces in Service APIs

Neither one is an easy pick, I must admit. Feel free to post better ideas into the comments.

(Option 3 doesnt work in Struts2 due the magic involved when converting request parameters)

|

share post

//

More articles in this subject area

Discover exciting further topics and let the codecentric world inspire you.

//

Gemeinsam bessere Projekte umsetzen.

Wir helfen deinem Unternehmen.

Du stehst vor einer großen IT-Herausforderung? Wir sorgen für eine maßgeschneiderte Unterstützung. Informiere dich jetzt.

Hilf uns, noch besser zu werden.

Wir sind immer auf der Suche nach neuen Talenten. Auch für dich ist die passende Stelle dabei.