title: Stateful session bean in Struts2
body: <p>I have been trying to create a simple stateful session bean with Struts2, but every time I resend my request and try to retrieve the info previously put in my stateful bean, it returns NULL. Am I missing something? </p>
  
  <p>The injection is working properly after the registration but when I make a new request with on the retrieve method in the action class, I am unable to get the info of my session since it returns null. </p>
  
  <p>So how do I do to achieve that?</p>
  
  <p>Pay no attention to the locks, I put them just to test other features. you can ignore them.I repeat the injection is working properly on the execute method</p>
  
  <pre class="lang-java prettyprint-override"><code>public class Registration extends ActionSupport implements ModelDriven&lt;Account&gt; {
      private static final long serialVersionUID = -8930461193700155653L;
      private Account account;
      private User user;
  
      @Inject // this is the interface implemented by my stateful bean
      AccountServiceLocal asl;
  
      @Inject
      PassHashInterface phi;
  
      @Inject
      UserServiceLocal usl;
      private final Lock lock =new ReentrantLock();
  
      @org.apache.struts2.interceptor.validation.SkipValidation
      public  String retrieve(){      
          if(asl.getEmail()!=null){ // always null        
              return "success";           
          }
          return "input";
      }
      public  String execute() throws NoSuchAlgorithmException,
                                      InvalidKeySpecException, 
                                      InterruptedException {
  
          user=new User();
          user.setEmail(account.getEmail().toString().trim());         
          System.out.println("in execute");
          account.setRole("Regular");
          account.setPwd(phi.createHash(account.getPwd()));
         /*
          * NB. the primary key in Account(AccountId) does not
          * Necessary matches the primary key in User(Id) 
          */         
          asl.create(account);
          usl.create(user);
          lock.unlock();          
          return "success";            
      }
  
      @Override
      public Account getModel() {
          account=new Account();
          return account;
      }
  }
  </code></pre>
  
  <p>My struts.xml file</p>
  
  <pre class="lang-xml prettyprint-override"><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE struts PUBLIC
      "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
      "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;         
      &lt;struts&gt;            
          &lt;constant name="struts.objectFactory.cdi.jndiKey" 
                   value="java:comp/some/weird/BeanManagerReference" /&gt;
          &lt;package name="default" namespace="/" extends="struts-default"&gt;
              &lt;action name="register" class="action.Registration"
                                     method="execute"&gt;
                  &lt;result name="success"&gt;success.jsp&lt;/result&gt; 
                  &lt;result name="input"&gt;registration.jsp&lt;/result&gt;
              &lt;/action&gt;                
              &lt;action name="save" class="action.Registration" 
                                 method="retrieve"&gt;
                  &lt;result name="success" &gt;index.jsp&lt;/result&gt; 
                  &lt;result name="input" &gt;registration.jsp&lt;/result&gt;
              &lt;/action&gt;               
          &lt;/package&gt;
      &lt;/struts&gt;
  </code></pre>
  
  <p>AccountserviceImpl:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Stateful
  public class AccountServiceImpl implements AccountServiceLocal {
  
      @Inject 
      private AccountDaoLocal adl;
      /* (non-Javadoc)
       * @see ejb.Business.AccountServiceLocal#create(ejb.Domain.Account)
       */
      @Override
      public void create(Account a) {
          // TODO Auto-generated method stub
          System.out.println("in"+this);
          adl.createdao(a);
      }
  
      /* (non-Javadoc)
       * @see ejb.Business.AccountServiceLocal#delete(java.lang.Object)
       */
      @Override
      public void delete(Object id) {
          // TODO Auto-generated method stub
  
      }
  
      /* (non-Javadoc)
       * @see ejb.Business.AccountServiceLocal#find(java.lang.Object)
       */
      @Override
      public Account find(Object id) {
          // TODO Auto-generated method stub
          return null;
      }
  
      /* (non-Javadoc)
       * @see ejb.Business.AccountServiceLocal#update(ejb.Domain.Account)
       */
      @Override
      public void update(Account a) {
          // TODO Auto-generated method stub
  
      }
  
      /* (non-Javadoc)
       * @see ejb.Business.AccountServiceLocal#findemail(java.lang.Object)
       */
      @Override
      public List findemail(Object id) {
          // TODO Auto-generated method stub
  
          return adl.finddaobyEmail(id);
      }
  
  }
  </code></pre>
  
  <p>AccountServiceLocal:</p>
  
  <pre class="lang-java prettyprint-override"><code>@Local
  public interface AccountServiceLocal {
  
      public void create(Account a);
  
      public void delete(Object id);
  
      public Account find(Object id);
  
      public List&lt;?&gt; findemail(Object id);
  
      public void update(Account a);
  
  }
  </code></pre>