EJB在企业应用程序中不被识别

发布于 2024-08-21 08:38:04 字数 3117 浏览 2 评论 0原文

我正在使用 Eclipse,我想使用 Glassfish 和 MySQL 创建一个企业应用程序。

我创建了一个企业应用程序项目,包含 EJB 和 WEB 模块,名为 WeatherEJB 和 WeatherWeb。

在 WeatherEJB 项目中,我使用 JPA 从表生成实体,并且还创建了一个名为 CountryDAO 的无状态远程会话 bean,它实现了 CountryDAOBean,以便包装生成的实体 Country。

在 WeatherWeb 项目中,我在 Java 构建池中添加了对 WeatherEJB 项目的引用、项目引用和模块依赖项。

然后,在 WeatherWeb 项目中,我创建了一个名为 CountryController 的托管 bean(在“请求”范围内),如下所示:

import javax.ejb.EJB;

import model.Country;

import service.CountryDAO;

public class CountryController 
{
    @EJB
    CountryDAO countryDao;

    private Country country;

    public CountryController()
    {
        country = new Country();
    }

    public String saveCountry()
    {
        String returnValue = "success";

        try
        {
            countryDao.saveCountry(country);
        }
        catch (Exception e){
            e.printStackTrace();
            returnValue = "failure";
        }
        return returnValue;
    }

    public Country getCountry(){
        return country;
    }

    public void setCountry(Country country){
        this.country = country;
    }
}

尽管我可以在 Glassfish 上成功部署该应用程序,但当我尝试访问使用 CountryController 的 jsf 时,我出现以下错误:

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.faces.FacesException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.naming.NameNotFoundException: service.CountryDAO#service.CountryDAO not found

我缺少什么?或者我做错了什么?

I am working on Eclipse and I want to create an enterprise application using Glassfish and MySQL.

I created a Enterprise Application Project, with EJB and WEB modules, named WeatherEJB and WeatherWeb.

In the WeatherEJB project I generated entities from tables, using JPA and also I created a stateless remote session bean, called CountryDAO, which implements CountryDAOBean, in order to wrap over the generated entity Country.

In the WeatherWeb project I added references to the WeatherEJB project in the Java Build bath, project references and module dependencies.

Then, in the WeatherWeb project, I created a managed bean called CountryController (at 'request' scope), which looks like this:

import javax.ejb.EJB;

import model.Country;

import service.CountryDAO;

public class CountryController 
{
    @EJB
    CountryDAO countryDao;

    private Country country;

    public CountryController()
    {
        country = new Country();
    }

    public String saveCountry()
    {
        String returnValue = "success";

        try
        {
            countryDao.saveCountry(country);
        }
        catch (Exception e){
            e.printStackTrace();
            returnValue = "failure";
        }
        return returnValue;
    }

    public Country getCountry(){
        return country;
    }

    public void setCountry(Country country){
        this.country = country;
    }
}

Although I can deploy successfully the application on Glassfish, when I try to access a jsf that uses the CountryController, I get the following errors:

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.faces.FacesException: javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.faces.FacesException: com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

com.sun.enterprise.InjectionException: Exception attempting to inject Unresolved Ejb-Ref managedBeans.CountryController/countryDao@jndi: service.CountryDAO@[email protected]@Session@null into class managedBeans.CountryController

root cause

javax.naming.NameNotFoundException: service.CountryDAO#service.CountryDAO not found

What am I missing? or what am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-08-28 08:38:04

而不是实现类:

@EJB
CountryDAO countryDao;

实际上,我应该使用接口

@EJB
CountryDAOBean countryDao;

Actually, instead of the implementation class:

@EJB
CountryDAO countryDao;

I should have used the interface:

@EJB
CountryDAOBean countryDao;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文