2013-10-05 141 views
0

我已經創建了一個具有JSF技術createBook.xhtml的Web應用程序,並且它使用託管Bean。在此頁面中,用戶必須填寫輸入字段中的所需信息,然後點擊提交按鈕。然後通過使用doCreateBook()方法將這些信息保存到數據庫中。我爲這個Web應用程序使用Tomcat 7.0服務器。javax.naming.NameNotFoundException:在此上下文中未綁定名稱[屬性名稱]。無法找到[班級名稱]

讓我們先啓動,錯誤消息我得到的,那就是:

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實體類中取出所有導入行,因爲它會使代碼片段變長。

回答

2

有幾件事情,這裏要注意:

  1. CDI和EJB不普通的Tomcat的工作,因爲Tomcat是隻有一個servlet容器,而不是一個真正的Java EE應用服務器一樣GlassfishJBoss。在this question你會發現一點點細節。

  2. 如果您確實想要使用CDI,則必須使用javax.enterprise.context包中的註釋(例如javax.enterprise.context.RequestScoped)。

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

+0

我安裝了TomEE,現在我的web應用程序可以工作。非常感謝你 :) –

相關問題