Struts2

From Resin 3.0

Revision as of 16:59, 9 February 2008 by Ferg (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

(The Struts2/Resin-IoC capability requires Resin 3.1.5)

Struts2 can work with Resin-IoC to enable all Struts actions with WebBeans-style injection. Your MyAction class can use the WebBeans @In and @Named annotations to retrieve beans defined in the resin-web.xml.

Optionally, you can define action classes as beans in the resin-web.xml to enable extra configuration if necessary.

The MyAction might look like:

 package example;

 import javax.webbeans.*;
 
 public class MyAction {
   @Named("wiki") DataSource _database;

   ...
 }

To enable Resin-IoC with Struts, you'll need to copy the resin/ext/resin-support.jar to either WEB-INF/lib or resin/ext-webapp. And you'll need to create or modify WEB-INF/classes/struts.properties as follows:

 # Tell struts to ask Resin for new action instances
 struts.objectFactory = com.caucho.xwork2.ResinObjectFactory

That's it. No other configuration is required to enable your actions for Resin-IoC.

ResinObjectFactory implementation

public class ResinObjectFactory extends ObjectFactory {
   private WebBeansContainer _webBeans = WebBeansContainer.create();

  @Override
  public Object buildBean(Class clazz, Map extraContext)
  {
    return _webBeans.getObject(clazz);
  }
}
Personal tools