请教一个初级的local访问出错的问题
最近才开始学ejb,出现一个local的bean无法访问的问题,大家帮忙看下
代码如下:
User类:
import java.io.Serializable;
public class User implements Serializable{
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserManager的本地接口
public interface UserManagerLocal {
public void addUser(User user);
}
UserManager的远程接口
public interface UserManagerRemote {
public void addUser(User user);
}
UserManagerBean的实现
@Stateless
@Local(UserManagerLocal.class)
@Remote(UserManagerRemote.class)
public class UserManagerBean implements UserManagerRemote,UserManagerLocal {
public void addUser(User user) {
System.out.println("User "+user.getUsername()+" saved");
user.setId(1);
}
}
接下来是客户端访问的代码:(jsp中代码,部署在和ejb同一个jboss中)
InitialContext context=new InitialContext();
UserManagerLocal userManager=(UserManagerLocal)context.lookup("UserManagerBean/local");
User u=new User();
u.setUsername("lee");
u.setPassword("123");
userManager.addUser(u);
out.println(u.getId());
但是始终一访问这个页面就会出错
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 20
17: User u=new User();
18: u.setUsername("lee");
19: u.setPassword("123");
20: userManager.addUser(u);
21: out.println(u.getId());
22: %>
23:
root cause
javax.ejb.EJBException: java.lang.IllegalArgumentException: Wrong target. class org.javen.ejb.UserManagerBean for public void org.javen.ejb.UserManagerBean.addUser(org.javen.ejb.User)
org.jboss.ejb3.tx.Ejb3TxPolicy.handleExceptionInOurTx(Ejb3TxPolicy.java:77)
org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:83)
org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
......
root cause
java.lang.IllegalArgumentException: Wrong target. class org.javen.ejb.UserManagerBean for public void org.javen.ejb.UserManagerBean.addUser(org.javen.ejb.User)
org.jboss.aop.joinpoint.MethodInvocation.handleErrors(MethodInvocation.java:151)
org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:126)
org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
......
但是,要是改成远程访问并修改相应代码,就没有问题。
这是怎么回事啊?有谁能解答下吗?
另外:如上例所示,大家一般是把@Remote等注解放在UserManagerRemote这样的接口里面,还是放在UserManagerBean这类实现的bean里面?具体工作中是怎么放的?
[解决办法]
解决了。。原来是不该把User,UserManagerRemote,UserManagerLocal打成jar包放在jsp client的lib目录下。
在工程的build path里面的project设到bean所在工程就可以了。
但是为什么呢?有谁能解释下吗?为什么拷贝了jar文件反而会出错?
[解决办法]
同楼主一样 我也出现了这个问题 我用的JBOSS6