javax.naming.NameNotFoundException:名称[attribute name]未绑定在此Context中。找不到[班级名称]

时间:2013-10-05 19:05:40

标签: jsf tomcat ejb

我创建了一个使用JSF技术createBook.xhtml的Web应用程序,它使用Managed Beans。在此页面中,用户必须在输入字段中填写所需信息,然后单击“提交”按钮。然后,应使用doCreateBook()方法将此信息保存到数据库中。我将Tomcat 7.0服务器用于此Web应用程序。

让我们首先看一下我得到的错误信息,即:

javax.naming.NameNotFoundException: Name [com.mysite.bookecommerce.beans.BookController/bookEJB] is not bound in this Context. Unable to find [com.mysite.bookecommerce.beans.BookController].

BookController是EJB,代码是:

package com.mysite.bookecommerce.beans;

import java.io.Serializable;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import com.mysite.bookecommerce.entities.Book;

@ManagedBean(name = "BookController")
@RequestScoped
public class BookController implements Serializable {

private static final long serialVersionUID = 1L;

@EJB
private BookEJB bookEJB;

private Book book = new Book();

public String doCreateBook() throws Exception{
    book = bookEJB.createBook(book);
    return "bookCreationSuccessful.xhtml";
}

public void doDeleteBook() throws Exception{
    bookEJB.deleteBook(book);
}

public void doUpdateBook() throws Exception {
    book = bookEJB.updateBook(book);
}

public BookController() {
    super();
}

}

BookEJB课程是:

package com.mysite.bookecommerce.beans;

import java.util.List;

import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import com.mysite.bookecommerce.entities.Book;

@Stateful
 public class BookEJB {

@PersistenceContext(unitName = "BookECommercePU", type = PersistenceContextType.EXTENDED)
private EntityManager em;

public Book createBook(Book book) throws Exception {
    em.persist(book);
    return book;
}

public void deleteBook(Book book) throws Exception {
    em.remove(book);
}

public Book updateBook(Book book) throws Exception {
    em.merge(book);
    return book;
}

@SuppressWarnings("unchecked")
public List<Book> getBooks() throws Exception {
    Query query = em.createQuery("SELECT b from Book AS b");
    return query.getResultList();
}
}

Book是实体类,代码为:

@Entity
@Table(name = "BOOKS")
public class Book {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long ID;

@Column(nullable = false)
private String name;

@Column(nullable = false, length = 2000)
private String description;

@Length(max = 10)
@Column(nullable = false)
private int isbn10;

@Length(max = 14)
private String isbn13;

@Column(nullable = false)
private String author;

private String publisher;
private int publishYear;

@Column(nullable = false)
private int numberOfPages;

@Column(nullable = false)
private double price;

private boolean illustrations;
private String dimension;
private double weight;
private int quantity;

 //getters and setters

还有faces-config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>

<faces-config
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">

<managed-bean>
    <managed-bean-name>BookController</managed-bean-name>
    <managed-bean-class>com.mysite.bookecommerce.beans.BookController</managed-bean-class>
    <managed-bean-scope>request</managed-bean-scope>
</managed-bean>

最后是web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>BookECommerce</display-name>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.faces</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
</servlet-mapping>

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

<context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
</context-param>
<context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.ALLOW_JAVASCRIPT</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.PRETTY_HTML</param-name>
    <param-value>true</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.DETECT_JAVASCRIPT</param-name>
    <param-value>false</param-value>
</context-param>

<context-param>
    <param-name>org.apache.myfaces.AUTO_SCROLL</param-name>
    <param-value>true</param-value>
</context-param>
<listener>
    <listener-class>org.apache.myfaces.webapp.StartupServletContextListener</listener-class>
</listener>

问题似乎是我的Web应用程序找不到BookController EJB。我怎么解决这个问题?我已经读过你必须在faces-config.xml文件中添加它但我添加了它仍然会收到此错误。

请注意,我从Book实体类中获取了所有导入行,因为它会使代码片段变长。

1 个答案:

答案 0 :(得分:2)

这里有几点需要注意:

  1. CDI和EJB不能与普通的Tomcat一起使用,因为Tomcat只是一个servlet容器而不是真正的Java EE应用服务器,如GlassfishJBoss。在this question中,您会找到更多详细信息。

  2. 如果您真的想使用CDI,则必须使用javax.enterprise.context包中的注释(例如javax.enterprise.context.RequestScoped)。

  3. faces-config.xml中的bean声明在JSF 2 +中是可选的。