Skip to main content

@EJB works in servlet, not in Struts Action

1 reply [Last post]
senderj
Offline
Joined: 2008-09-24

My env is Netbean with Glassfish. I am able to create a servlet to call EJB with this code:

public class TryAdvise_create extends HttpServlet {
@EJB
AdviseFacadeRemote advBean;
......
protected void processRequest(HttpServletRequest request, ........ {
......
Advise adv = new Advise(123, "123 Company");
advBean.create(adv);

It works great. But when doing the same on a Struts Action:

public class TryAdviseAction extends Action {
@EJB
AdviseFacadeRemote advBean;
......
public ActionForward execute(ActionMapping mapping, ......... {
Advise a = advBean.find(new Long(1));

advBean is always null. Why? Please help.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Adam Bien

glassfish@javadesktop.org schrieb:
> My env is Netbean with Glassfish. I am able to create a servlet to call EJB with this code:
>
> public class TryAdvise_create extends HttpServlet {
> @EJB
> AdviseFacadeRemote advBean;
> ......
> protected void processRequest(HttpServletRequest request, ........ {
> ......
> Advise adv = new Advise(123, "123 Company");
> advBean.create(adv);
>
> It works great. But when doing the same on a Struts Action:
>
> public class TryAdviseAction extends Action {
> @EJB
> AdviseFacadeRemote advBean;
> ......
> public ActionForward execute(ActionMapping mapping, ......... {
> Advise a = advBean.find(new Long(1));
>
> advBean is always null. Why? Please help.
>
Because of spec :-). Injection works only in dedicated Java EE
components. Struts actions are not a part of it.
There would be no problem with JSF backing beans :-).

You can easily use ServiceLocator instead: new
InitialContext().lookup(AdviceFacadeRemote.class.getName());

regards,

adam
> [Message sent by forum member 'senderj' (senderj)]
>
> http://forums.java.net/jive/thread.jspa?messageID=335811
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>
>

--
Consultant, Author, Java Champion

Homepage: www.adam-bien.com
Weblog: blog.adam-bien.com
eMail: abien@adam-bien.com
Mobile: 0049(0)170 280 3144

Books: Enterprise Architekturen (ISBN: 393504299X),
Java EE 5 Architekturen (ISBN: 3939084247),
J2EE Patterns, J2EE Hotspots, Enterprise Frameworks and Struts

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net