0

我创建了一个createBook.xhtml使用 JSF 技术并使用托管 Bean 的 Web 应用程序。在此页面中,用户必须在输入字段中填写所需信息,然后点击提交按钮。然后应该通过使用doCreateBook()方法将这些信息持久化到数据库中。我为此 Web 应用程序使用 Tomcat 7.0 服务器。

让我们首先从我收到的错误消息开始,即:

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();
}
}

书是实体类,代码是:

@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 实体类中获取了所有导入行,因为它会使代码片段变长。

4

1 回答 1

2

这里有几点需要注意:

  1. CDI 和 EJB 不适用于普通的 Tomcat,因为 Tomcat 只是一个 servlet 容器,而不是像GlassfishJBoss这样的真正的 Java EE 应用程序服务器。在这个问题中,您会发现更多细节。

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

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

于 2013-10-05T19:33:24.643 回答