Struts2 + Guice + Custom Type Conversion

Struts2 + Guice + Custom Type Conversion

If you use Google Guice you should be aware that the struts2-guice-plugin doesn't work properly for projects that use custom type conversion (e.g., converters registered in xwork-conversion.properties). If you find this affects you, here's a simple, alternate ObjectFactory that will work:

/**
 * A simplified Guice {@link ObjectFactory}. The factory that comes with the
 * guice-struts2 plug-in fails when using custom type converters. It does not
 * appear that this issue will be fixed in either Struts2 or the Guice plug-in
 * in the near future:
 * <p/>
 * https://issues.apache.org/jira/browse/WW-2898
 * https://github.com/google/guice/issues/278
 *
 * @author Steven Benitez
 */
public class GuiceObjectFactory extends ObjectFactory {
  private static final long serialVersionUID = 1L;

  @com.google.inject.Inject
  private static Injector injector;

  /**
   * Returns false, since Guice is capable of injecting parameters into
   * action constructors.
   */
  @Override
  public boolean isNoArgConstructorRequired() {
    return false;
  }

  @Override
  @SuppressWarnings("unchecked")
  public Object buildBean(final Class clazz, final Map extraContext) {
    return injector.getInstance(clazz);
  }
}

Remember to request static injection of this class in one of your Guice modules.

Mastodon