“类型”无法解析为类型,而在jsp中使用ejb

时间:2016-11-09 08:32:42

标签: java jsp ejb

我使用eclipse Neon 我使用以下代码创建了简单的EJB项目:

package com.clientManager;

import javax.ejb.LocalBean;
import javax.ejb.Stateless;

/**
 * Session Bean implementation class ClientManager
 */
@Stateless
@LocalBean
public class ClientManager {

    /**
     * Default constructor. 
     */
    public ClientManager() {
        // TODO Auto-generated constructor stub
    }

    public String test() {
        return "test";
    }
}  

它自动创建并驻留在ejbModule文件夹中 它使用EJB 3.2并在WildFly服务器上成功部署:

11:25:50,360 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-3) WFLYSRV0028: Stopped deployment Neoflex.jar (runtime-name: Neoflex.jar) in 9ms
11:25:50,370 WARN  [org.jboss.as.controller] (DeploymentScanner-threads - 2) WFLYCTL0357: Notification of type deployment-undeployed is not described for the resource at the address []
11:25:50,370 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0009: Undeployed "Neoflex.jar" (runtime-name: "Neoflex.jar")
11:25:55,380 INFO  [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) WFLYDS0004: Found Neoflex.jar in deployment directory. To trigger deployment create a file called Neoflex.jar.dodeploy
11:25:55,380 INFO  [org.jboss.as.server.deployment] (MSC service thread 1-7) WFLYSRV0027: Starting deployment of "Neoflex.jar" (runtime-name: "Neoflex.jar")
11:25:55,400 INFO  [org.jboss.weld.deployer] (MSC service thread 1-6) WFLYWELD0003: Processing weld deployment Neoflex.jar
11:25:55,400 INFO  [org.jboss.as.ejb3.deployment] (MSC service thread 1-6) WFLYEJB0473: JNDI bindings for session bean named 'ClientManager' in deployment unit 'deployment "Neoflex.jar"' are as follows:

    java:global/Neoflex/ClientManager!com.clientManager.ClientManager
    java:app/Neoflex/ClientManager!com.clientManager.ClientManager
    java:module/ClientManager!com.clientManager.ClientManager
    java:global/Neoflex/ClientManager
    java:app/Neoflex/ClientManager
    java:module/ClientManager

11:25:55,523 INFO  [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed "Neoflex.jar" (runtime-name : "Neoflex.jar")

现在我为JSP页面创建了DynamicWebProject,它将使用EJB:

<%@page import="com.clientManager.*"%>
<%
    ClientManager c = new ClientManager(); 
%>
<%= c.test() %>

我在JSP项目的Build Path中配置了EJB项目(!)
请看截图。

screenshot

当我发布JSP并访问页面(main.jsp)时,我有:

JBWEB004060: An error occurred at line: 3 in the jsp file: /main.jsp
ClientManager cannot be resolved to a type
1: <%@page import="com.clientManager.*"%>
2: <%
3:  ClientManager c = new ClientManager(); 
4: %>
5: <%= c.test() %>

有什么问题?

1 个答案:

答案 0 :(得分:1)

您不应该直接实例化EJB bean而不是EJB容器管理的生命周期,获取EJB bean句柄的正确方法是通过它的查找方法。

确保选择正确的查找名称,我随机使用您帖子中的名称 尝试更改为以下内容。

<%
    ClientManager cm;
    cm = (ClientManager) ctx.lookup("java:module/ClientManager");
%>