简体   繁体   English

创建一个EJB3.1 + Portlet项目

[英]Creating a EJB3.1 + Portlet Project

I just want to create a Portlet and to use an EJB in this Portlet. 我只想创建一个Portlet并在此Portlet中使用EJB。

Iam using JBoss 7.1 and Liferay 6.2. Iam使用JBoss 7.1和Liferay 6.2。

I create a EJB project and a liferay plugin project. 我创建了一个EJB项目和一个liferay插件项目。

I just want to call a method from the EJB, shown here: 我只想从EJB调用一个方法,如下所示:

@Local
public class PortletController {

 public PortletController() {
 }

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

My portlet tries to get the username, shown here: 我的portlet尝试获取用户名,如下所示:

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. 我在一个教程中读到,如果bean在与portlet相同的JRE中运行,则无需查找即可访问本地bean。

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: 我将EJB项目导出为jar,并将其作为依赖项添加到portlet项目,但是在以下代码行中,我只是在doView方法中得到了NullpointerException:

        userName = controller.getUserName();

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. 我在一个教程中读到,如果bean在与portlet相同的JRE中运行,则无需查找即可访问本地bean。

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. 提到JRE时 ,您的意思是在同一个JVM中,是的,如果您的portlet和ejb模块都在同一JVM实例中本地运行,那么您就不必为JNDI查找而烦恼。

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). 关于打包应用程序的方式,我建议不要这样做(ejb和portlet在同一jar中),因为从最佳实践中可以知道,最好将业务模块 (ejb)与视图模块 (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. 因此,您可能需要分别打包每个组件,将portlet转到war归档中,并将ejb放入其自己的jar / module中。

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: 使用POJI声明您的本地bean骨架:

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

Implement your Stateless/Stateful session bean and do specify its name annotation property: 实现您的Stateless/Stateful会话bean,并指定其name注释属性:

@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: 使用beanName属性在portlet控制器类下注入bean:

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);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM