<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>spiros.blog() &#187; Struts</title>
	<atom:link href="http://www.tzavellas.com/techblog/tag/struts/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tzavellas.com/techblog</link>
	<description>Spiros Tzavellas’s blog, mostly on software development and Java.</description>
	<lastBuildDate>Sun, 22 Aug 2010 20:25:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Using Dependency Injection in Struts2 for stateless EJBs part 2</title>
		<link>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/</link>
		<comments>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/#comments</comments>
		<pubDate>Tue, 03 Jul 2007 10:10:12 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/</guid>
		<description><![CDATA[This is the second, and last part of the Using Dependency Injection in Struts2 for stateless EJBs series of posts. In this post I will present a utility class that can be used to make the creation of Guice bindings for EJB3s easier. Introducing EjbBinder Our goal is to make an easier API, specifically for [...]]]></description>
			<content:encoded><![CDATA[<p>This is the second, and last part of the <strong>Using Dependency Injection in Struts2 for stateless EJBs</strong> series of posts. In this post I will present a utility class that can be used to make the creation of Guice bindings for EJB3s easier.</p>
<h3>Introducing <code>EjbBinder</code></h3>
<p>Our goal is to make an easier API, specifically for creating bindings of EJB3s, and make the specification of the JNDI name optional, when possible. The below code is a new version of the <code>EjbModule</code> class that uses a new class, called <code>EjbBinder</code>, that we are going to present in this post.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EjbModule <span style="color: #000000; font-weight: bold;">implements</span> Module <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span>Binder binder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Bind Context to the default InitialContext.</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">InitialContext</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        EjbBinder ejbs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EjbBinder<span style="color: #009900;">&#40;</span>binder, <span style="color: #000000; font-weight: bold;">new</span> GlassfishEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	ejbs.<span style="color: #006633;">bindRemote</span><span style="color: #009900;">&#40;</span>CurrencyManagerRemote.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	ejbs.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>EjbBinder</code> contains two methods named <code>bind</code> and <code>bindRemote</code>. The <code>bind</code> method receives as arguments the expected class and JNDI name and simply uses <code>Binder</code> and <code>JndiItegration</code> to create a binding. The <code>bindRemote</code> method takes as an argument the remote interface of a sesion EJB3 and creates a binding using a JNDI name retrieved from the <code>EjbJndiNameStrategy</code>, that is specified in the <code>EjbBinder</code> constructor (BTW the <code>CurrencyManagerRemote</code> EJB interface in the above code does not relate with the application that we developed in the previous post and it is here to illustrate the <code>bindRemote</code> method).</p>
<p>The definition of the <code>EjbJndiNameStrategy </code>interface:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The implementation of <code>EjbBinder</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Binder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.binder.ScopedBindingBuilder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">inject</span>.<span style="color: #006633;">jndi</span>.<span style="color: #006633;">JndiIntegration</span>.<span style="color: #006633;">fromJndi</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EjbBinder <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Binder binder<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> EjbJndiNameStrategy jndiNameStrategy<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> EjbBinder<span style="color: #009900;">&#40;</span>Binder binder, EjbJndiNameStrategy nameStrategy<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">binder</span> <span style="color: #339933;">=</span> binder<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">jndiNameStrategy</span> <span style="color: #339933;">=</span> nameStrategy<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> ScopedBindingBuilder bindRemote<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> bind<span style="color: #009900;">&#40;</span>beanInterface,
                jndiNameStrategy.<span style="color: #006633;">interfaceToJndiName</span><span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> ScopedBindingBuilder bind<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;</span>t<span style="color: #339933;">&gt;</span> beanInterface, <span style="color: #003399;">String</span> jndiName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">toProvider</span><span style="color: #009900;">&#40;</span>fromJndi<span style="color: #009900;">&#40;</span>beanInterface, jndiName<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Application Server Specific Naming Strategies</h3>
<p>When you create a new EJB and you do not specify a JNDI name, the application server assigns a default name for you. So, if you have an implementation of <code>EjbJndiNameStrategy</code> that uses the naming rules of your application server you could avoid the need to specify a JNDI name when creating the EJB and when creating the binding of the EJB to Guice.</p>
<p>Below we have two implementations of the <code>EjbJndiNameStrategy</code> that can infer the JNDI name of an EJB&#8217;s remote client interface using the remote interface&#8217;s class for the Glassfish and JBoss application servers.</p>
<p>For the Glassfish application server the default global JNDI name of an EJB with a remote interface is the <a href="https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#SessionBeanGlobalJNDINameAssignment">fully qualified name of the remote interface</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> GlassfishEjbJndiNameStrategy <span style="color: #000000; font-weight: bold;">implements</span> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> beanInterface.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For the JBoss application server the default JNDI name for the remote interface of an EJB is <em>earName/beanName/remote</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//WARNING: Not tested!</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> JbossEjbJndiNameStrategy <span style="color: #000000; font-weight: bold;">implements</span> EjbJndiNameStrategy <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> earName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JbossEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JbossEjbJndiNameStrategy<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> earName<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>earName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #0000ff;">&quot;&quot;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>earName.<span style="color: #006633;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">earName</span> <span style="color: #339933;">=</span> earName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> interfaceToJndiName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> earName <span style="color: #339933;">+</span> interfaceToBeanName<span style="color: #009900;">&#40;</span>beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;/remote&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #003399;">String</span> interfaceToBeanName<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> beanInterface<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003399;">String</span> name <span style="color: #339933;">=</span> beanInterface.<span style="color: #006633;">getSimpleName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>name.<span style="color: #006633;">endsWith</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remote&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            name <span style="color: #339933;">=</span> name.<span style="color: #006633;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Remote&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> name <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;Bean&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using Dependency Injection in Struts2 for stateless EJBs part 1</title>
		<link>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/</link>
		<comments>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/#comments</comments>
		<pubDate>Tue, 03 Jul 2007 09:38:10 +0000</pubDate>
		<dc:creator>spiros</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[EJB]]></category>
		<category><![CDATA[Guice]]></category>
		<category><![CDATA[Struts]]></category>

		<guid isPermaLink="false">http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/</guid>
		<description><![CDATA[In this post I will present a way to use dependency injection of stateless session beans in Struts 2 actions using Guice. I will only write about EJB3, because they have simpler client lookup code, but you might be able to change the presented code to cover older versions of EJB. We will use off [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I will present a way to use dependency injection of stateless session beans in Struts 2 actions using Guice. I will only write about EJB3, because they have simpler client lookup code, but you might be able to change the presented code to cover older versions of EJB.</p>
<p>We will use off the shelf Struts 2 and off the shelf Guice (with the Guice Struts 2 plug-in). No extra &#8220;glue&#8221; code is required to inject stateless EJBs into a Struts actions with Guice. In the next post I will tweak the presented code and make the binding of EJBs in Guice modules simpler.</p>
<p>This post is divided in two sections. In the first section we will create a simple, complete, hello-world type application using Struts 2. The application will consist of only one action that when called will print the current date in a JSP page. Then we will write a stateless session EJB and use Guice to inject the EJB into the action. If you are already familiar with Struts 2 you might want to skip the first section and go directly to the second.</p>
<h2>A basic Struts 2.x application</h2>
<p>Below you will find a series of steps with all code for our mini application.</p>
<h3>1. Project setup</h3>
<p>With your favorite IDE create an enterprise project. You need to create an enterprise project (ear based deployment) and not a web project (war based deployment) because we will create an EJB in the next section.</p>
<p>Now add a web module into your enterprise project and add the following jars to <em>WEB-INF/lib</em>: commons-logging-1.1.jar, freemarker-2.3.8.jar, ognl-2.6.11.jar, struts2-core-2.0.8.jar and xwork-2.0.3.jar.</p>
<p>Although I am a happy Eclipse user I have to admit that Eclipse has very basic support for Java EE 5, so for the code of this post I used Netbeans 6.0 M9 with Glassfish 2 Beta as the application server. Netbeans IMHO is not so good for Java editing but has decent support for the latest Java EE standards. Please keep in mind that the code is only tested with Glassfish 2 Beta.</p>
<h3>2. Edit web.xml</h3>
<p>The below code is the minimum web.xml needed for a Struts 2 application. It contains the Struts 2 filter definition (<code>FilterDispatcher</code>) and a welcome file (<em>index.html</em>) that it is used to redirect the user to the default Struts 2 action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;web-app</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.5&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.struts2.dispatcher.FilterDispatcher<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>struts2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url-pattern<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/filter-mapping<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>index.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/welcome-file-list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/web-app<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>3. Create struts.xml</h3>
<p>Now we need to create the struts.xml file. This file is located at the root of your classpath. The below code has some basic configuration parameters and includes the <em>dates</em> Struts package that will contain our action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts PUBLIC</span>
<span style="color: #00bbdd;">    &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.enable.DynamicMethodInvocation&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.devMode&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;false&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Include the package with our actions --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;dates.xml&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>4. Create the dates Struts package</h3>
<p>Create the dates.xml file at the root of your classpath as we specified in the above struts.xml. In this file we define a package named <em>dates</em> and mapped under <em>&lt;context-root&gt;/dates/*</em> URL.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE struts PUBLIC</span>
<span style="color: #00bbdd;">        &quot;-//Apache Software Foundation//DTD Struts Configuration 2.0//EN&quot;</span>
<span style="color: #00bbdd;">        &quot;http://struts.apache.org/dtds/struts-2.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;package</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dates&quot;</span> <span style="color: #000066;">namespace</span>=<span style="color: #ff0000;">&quot;/dates&quot;</span> <span style="color: #000066;">extends</span>=<span style="color: #ff0000;">&quot;struts-default&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;CurrentDate&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;com.tzavellas.dates.web.CurrentDateAction&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/jsp/date.jsp<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/result<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/action<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Add more actions here --&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/package<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/struts<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The package contains our only action, named <code>CurrentDate</code> and mapped to the URL <em>&lt;context-root&gt;/dates/CurrentDate.action</em>.</p>
<h3>5. Create the welcome file</h3>
<p>Now we will create our welcome file that redirects the user to the Struts action. This is a simple HTML page named index.html (as defined in web.xml) and located under the context root. The page uses a meta tag to redirect to the URL of the action .</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Refresh&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;0;URL=dates/CurrentDate.action&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Loading ...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/p<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>6. Code the action</h3>
<p>Finally it&#8217;s time for some Java code. The below code is a simple Struts 2 action that when called will set the value of the <code>currentDate</code> property to the current date.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.opensymphony.xwork2.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CurrentDateAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        currentDate = <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> SUCCESS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> getCurrentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> currentDate<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>/code<span style="color: #339933;">&gt;</span></pre></div></div>

<p>Note that unlike Servlets or Struts 1 actions, in Struts 2 for every HTTP request a new action instance is created so it is safe to mutate an action property (like the <code>currentDate</code> above) with request specific data.</p>
<h3>7. Create the view</h3>
<p>As we defined in the dates package (<em>dates.xml</em>) the view for the <code>CurrentDateAction</code> is a JSP file localed in <em>WEB-INF/jsp/date.jsp</em>. The below code, for the <em>date.jsp</em> page, uses the Struts 2 tag library to display the value of the <code>currentDate</code> property of the action.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;">&lt;%@ page <span style="color: #000066;">contentType</span>=<span style="color: #ff0000;">&quot;text/html&quot;</span> <span style="color: #000066;">pageEncoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;">&lt;%@ taglib <span style="color: #000066;">prefix</span>=<span style="color: #ff0000;">&quot;s&quot;</span> <span style="color: #000066;">uri</span>=<span style="color: #ff0000;">&quot;/struts-tags&quot;</span> %<span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;</span>
<span style="color: #00bbdd;">   &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;meta</span> <span style="color: #000066;">http-equiv</span>=<span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span>=<span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The current date<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/head<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>The current date is: <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;s:property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;currentDate&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/h1<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/body<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>We&#8217;ve just created a simple application with Struts 2. In the next section we are going to define a stateless session EJB3 and wire it with the above action using Guice.</p>
<p>For more information about the Struts 2 web framework visit the <a href="http://cwiki.apache.org/WW/home.html">Struts 2 wiki</a> pages.</p>
<h2>The EJB and the integration with Guice</h2>
<h3>8. Create the stateless session EJB3</h3>
<p>With your favorite IDE add an EJB module to the enterprise project we created in <em>step 1</em>.</p>
<p>Below we define the SLSB with a remote interface and one method that returns the current date. The bean is mapped to the global JNDI name <em>com.tzavellas.dates.ejb.DatesManagerBean</em>.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.ejb</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Stateless</span><span style="color: #339933;">;</span>
&nbsp;
@Stateless<span style="color: #009900;">&#40;</span>mappedName=<span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DatesManagerBean <span style="color: #000000; font-weight: bold;">implements</span> DatesManagerRemote <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399; font-weight: bold;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.ejb</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.ejb.Remote</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399; font-weight: bold;">Remote</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> DatesManagerRemote <span style="color: #009900;">&#123;</span>
    <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>9. Add the Guice jars in the classpath</h3>
<p>Add guice-1.0.jar and guice-struts2-plugin-1.0.1.jar in <em>WEB-INF/lib</em> directory of your web module. The jar file guice-1.0.jar contains the core Guice implementation and  the guice-struts2-plugin-1.0.1.jar file contains the Guice&#8217;s Struts 2 integration code.</p>
<h3>10. Edit struts.xml to include the Guice plug-in</h3>
<p>Struts 2 is a very flexible framework. One of Struts&#8217; many extensions is the <code>ObjectFactory</code> plug-in interface. The responsibility of an  <code>ObjectFactory</code> is to create the objects managed by Struts. An <code>ObjectFactory</code> is usually a dependency injection container like Spring, Pico and Guice.</p>
<p>To define Guice as the <code>ObjectFactory</code> of our Struts 2 application we have to set the <em>struts.objectFactory</em> property to <em>guice</em> in the struts.xml file and Guice will then be responsible for instantiating all the Struts managed objects (actions, interceptors, etc) .</p>
<p>To continue add the following lines in struts.xml.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- Use Guice as the ObjectFactory for this application --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;struts.objectFactory&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;guice&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #808080; font-style: italic;">&lt;!-- When wiring actions use Guice bindings defined in the below module --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constant</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;guice.module&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;com.tzavellas.dates.web.EjbModule&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<h3>11. Code the EjbModule</h3>
<p>In the above struts.xml we defined two properties, <code>struts.objectFactory</code> and <code>guice.module</code>. To explain the <code>guice.module</code> property and the concept of Guice modules we have to explain the basics of Guice.</p>
<p>Guice is a, Java 5 based, dependency injection container that does constructor, field and setter injection (Guice also has features like support for AOP Alliance interceptors that we are not going to talk about in this post). To configure Guice you create a set of modules. A module implements the <code>com.google.inject.Module</code> interface and contains bindings. Bindings are created with the <code>com.google.inject.Binder</code> that is passed to the <code>configure(Binder)</code> method of a module. A binding is a pair composed of a type (key) and an implementation. An implementation can be an object, a class or a provider (factory). The bindings are consulted by the Guice <code>Injector</code> when doing dependency injection.</p>
<p>In the Guice Struts 2 plug-in the Guice <code>ObjectFactory</code> creates an <code>Injector</code> that then reads the module from the <code>guice.module</code> property in the <em>struts.xml</em> file and uses the bindings that are specified in that module to do dependency injection in Struts 2 actions and interceptors.</p>
<p>All we have to do to enable our EJB to be injected into an action is to create a binding for the client interface of the bean. We can&#8217;t create a binding to a class or instance, because the EJB&#8217;s instances must be instantiated by the EJB container and not by Guice, so we will use a provider that will lookup the EJB from JNDI. The Guice library includes a JNDI provider that we are going to use that it is located in the <code>com.google.inject.jndi.JndiIntegration</code> class.</p>
<p>The below code is the implementation of the <code>EJBModule</code>, which has  only two bindings. The first binds the <code>javax.naming.Context</code> type to the <code>javax.naming.InitialContext</code> implementation. This means that when the Guice <code>Injector</code> finds a field or parameter of type <code>javax.naming.Context</code>, that needs to be injected (marked with the <code>@Inject</code> annotation), it will create a new object of type <code>javax.naming.InitialContext</code> and inject it. This binding is needed by the JNDI provider that we are going to use in the next binding.</p>
<p>The second binding binds our EJB&#8217;s remote interface to the JNDI provider. The JNDI provider is returned from the call to the static <code>JndiIntegration.fromJndi(Class, String)</code> method. To get a JNDI provider we have to specify the expected class and JNDI name. This binding tells the Guice <code>Injector</code> that when it finds a field or parameter of type <code>com.tzavellas.dates.ejb.DatesManagerRemote</code>, that needs to be injected (marked with the <code>@Inject</code> annotation), it should call the provider&#8217;s <code>get()</code> method to retrieve an instance to inject.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Binder</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Module</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> com.<span style="color: #006633;">google</span>.<span style="color: #006633;">inject</span>.<span style="color: #006633;">jndi</span>.<span style="color: #006633;">JndiIntegration</span>.<span style="color: #006633;">fromJndi</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.tzavellas.dates.ejb.DatesManagerRemote</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.naming.InitialContext</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> EjbModule <span style="color: #000000; font-weight: bold;">implements</span> Module <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #006600; font-weight: bold;">void</span> configure<span style="color: #009900;">&#40;</span>Binder binder<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #666666; font-style: italic;">// Bind Context to the default InitialContext.</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">Context</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #003399; font-weight: bold;">InitialContext</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// Bind the remote interface to the JNDI name using the JNDI Provider</span>
        binder.<span style="color: #006633;">bind</span><span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
                .<span style="color: #006633;">toProvider</span><span style="color: #009900;">&#40;</span>fromJndi<span style="color: #009900;">&#40;</span>DatesManagerRemote.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #0000ff;">&quot;com.tzavellas.dates.ejb.DatesManagerBean&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For more information on how to do dependency injection with Guice see the <a href="http://docs.google.com/View?docid=dd2fhx4z_5df5hw8">Guice User&#8217;s Guide</a>.</p>
<h3>12. Change the action to use the EJB</h3>
<p>Now that Guice instantiates our actions we can use the normal Guice <code>@Inject</code> annotation to mark an injection point in the action class. We can use any of the constructor, setter and field injection mechanisms. The below code uses field injection because it might be more familiar to EJB3 developers.</p>

<div class="wp_syntax"><div class="code"><pre class="java5" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.tzavellas.dates.web</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.google.inject.Inject</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.opensymphony.xwork2.ActionSupport</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.tzavellas.dates.ejb.DatesManagerRemote</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CurrentDateAction <span style="color: #000000; font-weight: bold;">extends</span> ActionSupport <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Guice field injection of the SLSB</span>
    @Inject DatesManagerRemote bean<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399; font-weight: bold;">Date</span> currentDate<span style="color: #339933;">;</span>
&nbsp;
    @<span style="color: #003399; font-weight: bold;">Override</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">String</span> execute<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399; font-weight: bold;">Exception</span> <span style="color: #009900;">&#123;</span>
        currentDate = bean.<span style="color: #006633;">currentDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> SUCCESS<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399; font-weight: bold;">Date</span> getCurrentDate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> currentDate<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Conclusion</h3>
<p>We are done. We&#8217;ve just coded a simple Struts 2 application that uses Guice to inject stateless session EJB3s to Struts actions. You can follow the instructions outlined above to do the same into your applications.</p>
<h3>Appendix: Quick summary of all the steps</h3>
<p>Assuming that you have a working Struts 2 application, below you will find a series of steps you could follow to do dependency injection of your stateless EJB3s into your Struts 2 actions.</p>
<p>For your project:</p>
<ol>
<li>Include the Guice jars in the <em>WEB-INF/lib</em> directory.</li>
<li>Define a Guice module that will contain the bindings for your EJBs.</li>
<li>Edit the struts.xml to define Guice as your <code>ObjectFactory</code> and include the above module.</li>
</ol>
<p>For every EJB that you want to inject into your Struts actions:</p>
<ol>
<li>Inside the module, define a binding for the EJB using the Guice <code>Provider</code> from <code>com.google.inject.jndi.JndiIntegration</code> class, by specifying the bean&#8217;s client interface and the global JNDI name of the bean (or an ejb-ref if you have one defined in web.xml).</li>
<li>Use the <code>@Inject</code> annotation in the Struts actions where you want the EJBs injected.</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.tzavellas.com/techblog/2007/07/03/using-dependency-injection-in-struts2-for-stateless-ejbs-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
