Question

I just want to create a Portlet and to use an EJB in this Portlet.

Iam using JBoss 7.1 and Liferay 6.2.

I create a EJB project and a liferay plugin project.

I just want to call a method from the EJB, shown here:

@Local
public class PortletController {

 public PortletController() {
 }

 public String getUserName() {
  return "foobar";      
 }
}

My portlet tries to get the username, shown here:

public class ABPortlet extends MVCPortlet {

private String userName;

@EJB
PortletController controller;


public ABPortlet() {}

public void doView(RenderRequest rr, RenderResponse rp) throws IOException, PortletException {

    userName = controller.getUserName();
    if(userName==null) {
        userName = "nope";
    }
    rr.setAttribute("userName", userName);
    super.doView(rr, rp);
}
}

Have I already done something wrong? I read in a tutorial that i can access a local bean without lookup if the bean runs in the same JRE like the portlet.

How do i deploy both projects correctly? I exported the EJB project as jar and added it as dependency to the portlet project, but I just got a NullpointerException in the doView methode, at this line:

        userName = controller.getUserName();
Was it helpful?

Solution

I read in a tutorial that i can access a local bean without lookup if the bean runs in the same JRE like the portlet.

You meant in the same JVM when you mentioned the JRE, and yes that is right you don't have to bother about JNDI lookups if your portlet and ejb module runs both locally in the same JVM instance.

Regarding the way you packaged your application, I will advice not to do so (ejb and portlet in the same jar) because it is known from best practices that you would better separate your business module (ejb) from you view module (portlet). So you may need to package each separately, the portlet goes to a war archive and your ejb to its own jar/module one.

Now going back to your code, you have some things to review following enterprise archive coding conventions:

Use a POJI to declare your local bean skeleton:

@Local
public interface PortletControllerLocal 
{
  public String getUserName();
}

Implement your Stateless/Stateful session bean and do specify its name annotation property:

@Statefull
@EJB(name = "portletControllerBean")
public class PortletControllerBean implements PortletConrollerLocal
{
  public String getUsername()
  {
    //Do you stuff
  }
}

Inject your bean under your portlet controller class with a beanName property:

public class ABPortlet extends MVCPortlet 
{
  private String userName;
  @EJB(beanName = "portletControllerBean")
  PortletControllerBean controller;

  public ABPortlet() {}

  public void doView(RenderRequest rr, RenderResponse rp) throws IOException,PortletException 
  {
    userName = controller.getUserName();
    if(userName==null) {
      userName = "nope";
    }
    rr.setAttribute("userName", userName);
    super.doView(rr, rp);
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top