Using the Guice Struts 2 plugin


Guice 3.0 was released a few days ago!  One of the easiest ways to use it in your web server is to use Struts 2 with the Struts 2 plugin, which is available in the central Maven repository.

This tutorial assumes familiarity with Guice and Struts 2.

In order to use it the plugin, your injector must be created with a Struts2GuicePluginModule:

Injector injector = Guice.createInjector(
    new MyModule());

You must then define a GuiceServletContextListener to provide the injector to the Struts 2 plugin. I injected the Injector because I’m using embedded Jetty. However, if you’re using a standard servlet container, you’d probably just create the injector in the class itself.

package com.benmccann.example;


 * @author
public class GuiceListener extends GuiceServletContextListener {

  private final Injector injector;

  public GuiceListener(Injector injector) {
    this.injector = injector;

  public Injector getInjector() {
    return injector;


You must then wire it up in your web.xml:




There’s also an example in the Guice source code repository.


Be Sociable, Share!