大家好,最近开始看《经典Java EE企业应用实战》一书,其中有个例子关于本地调用ejb,没试成功,请帮忙看下问题出在哪里,谢谢。
这是一个Myeclipse中的一个ejb项目,其中CatService.java定义了一个Local接口CatService。CatServiceBean.java是这个接口的实现类。Person和Cat是其中用到的两个自定义类。经编译和打包后,在build下生成CatService.jar文件。将该文件放到wildfly-8.1.0.CR2\standalone\deployments\下,自动部署成功,提示:
13:04:21,291 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-7) JNDI bindings for session bean named CatServiceBean in deployment unit deployment "CatService.jar" are as follows:
java:global/CatService/CatServiceBean!org.crazyit.service.CatService
java:app/CatService/CatServiceBean!org.crazyit.service.CatService
java:module/CatServiceBean!org.crazyit.service.CatService
java:global/CatService/CatServiceBean
java:app/CatService/CatServiceBean
java:module/CatServiceBean
接着在Myeclipse里创建另一个Web应用,想在此项目里调用上面的EJB。
其中的index.jsp如下:
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="org.crazyit.service.* , org.crazyit.business.*"%>
<%@ page import="javax.naming.*"%>
<%
InitialContext ctx = new InitialContext();
//通过JNDI查找获得EJB
Object stub = ctx.lookup("CatServiceBean/local");
CatService cs = (CatService)stub;
Person p = new Person(1 , "孙悟空");
//调用EJB的业务方法
Cat[] cats = cs.getCats(new Person(2 , "猪八戒"));
//查看调用EJB业务方法的返回结果
for(int i = 0 ; i < cats.length ; i++)
{
out.println(cats[i].getName() + "的年龄:"
+ cats[i].getAge() + "<br/>");
}
%>
最后在Myeclipse里启动该项目时,console里提示:
14:41:15,717 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /CatServiceTest/index.jsp: org.apache.jasper.JasperException: JBWEB004062: Unable to compile class for JSP:
JBWEB004060: An error occurred at line: 9 in the jsp file: /index.jsp
CatService cannot be resolved to a type
6: //通过JNDI查找获得EJB
7: //Object stub = ctx.lookup("CatServiceBean/local");
8: Object stub = ctx.lookup("java:module/CatServiceBean");
9: CatService cs = (CatService)stub;
10: Person p = new Person(1 , "孙悟空");
11: //调用EJB的业务方法
12: Cat[] cats = cs.getCats(new Person(2 , "猪八戒"));
问题出在红色这句,index.jsp里明明import了 org.crazyit.service.* 和 org.crazyit.business.* ,为什么还是cannot be resolved呢?