1

I have a problem calling EJB object in struts action.

I deployed my application in glassfish, in application description of glassfish admin console I see that there is one StatelessSessionBean deployed. My application's .ear file consist of .war (web module) and .jar (ejb), one message-driven, one session bean.

When I try to call session bean in struts action class I get nullpointer exception.

Here is my call:

@EJB
private AccountFacade accountFacade;

@Override
public ActionForward execute(ActionMapping mapping,
                             ActionForm form,
                             HttpServletRequest request,
                             HttpServletResponse response)
        throws Exception {
    UserCreationForm userCreationForm = (UserCreationForm) form;

    Account account = new Account();
    account.setName(userCreationForm.getName());
    account.setEmail(userCreationForm.getEmail());
    account.setPassword(userCreationForm.getPassword());

    accountFacade.create(account);

    return mapping.findForward(NavigationUtils.ACTION_SUCCESS);
}

Exception occurs at this line: accountFacade.create(account);

Account facade class looks like this:

@Stateless
public class AccountFacade extends AbstractFacade<Account> implements AccountFacadeLocal {

    /**
     * Persistence context entity manager.
     */
    @PersistenceContext(unitName = "SearchEnginePU")
    private EntityManager em;

    /**
     * Gets entity manager.
     *
     * @return entity manager.
     */
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    /**
     * Constructor.
     */
    public AccountFacade() {
        super(Account.class);
    }

}

AccountFacadeLocal interface:

@Local
public interface AccountFacadeLocal {

    void create(Account account);

    void edit(Account account);

    void remove(Account account);

    Account find(Object id);

    List<Account> findAll();

    int count();

}

What am I missing here?

Paulius Matulionis
  • 23,085
  • 22
  • 103
  • 143

1 Answers1

3

A Struts action is not a standard Java EE web component, and is not instantiated and managed by the Java EE container, so EJBs are not injected in Struts actions.

Use JNDI to lookup your beans, or use http://code.google.com/p/struts-di/ (not tested). Also see EJB 3.1: Does it allow injection of beans into resources not managed by the container? for a similar question.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks, you helped me solve my problem. This is the first time I am using EJB in struts application, so I did not know this. I have used JNDI look up and every works just fine. – Paulius Matulionis Apr 18 '12 at 19:39