@ManagedProperty注释类型返回null

我有这个服务bean:
@Stateless
public class BookService
{
    @PersistenceContext(unitName="persistentUnit")
    protected EntityManager entityManager;

    public BookModel find(Long id) {
       return entityManager.find(BookModel.class, id);
    }
}
Facelet页面的支持bean是:
@ManagedBean(name = "bookBean")
@RequestScoped
public class BookBean implements Serializable
{
    @EJB
    private BookService bookService;

    @ManagedProperty(value="#{param.id}")
    private Long id;

    private DataModel<BookModel> books;
    private BookModel currentBook;

    @PostConstruct
    public void init() {
        if (id == null) {
           // UPDATE: Retrieve a list of books.
        } else { 
           // UPDATE: id shouldn't be null here.
           // Get detail info about a book using the id
           currentBook = bookService.find(id);
        }
    }

    public Long getId() {
       return id;
    } 

    public void setId(Long id) {
       this.id = id;
    } 

    public BookModel getCurrentBook() {
       return currentBook;
    }

    public void setCurrentBook(BookModel currentBook) {
       this.currentBook = currentBook;
    }
 }
为什么
id
的值总是返回null,即使URL返回为
bookedit.jsf?id=5418
我也不明白这一点。 此外,我发现
EntityManager#find
方法相当严格,因为它只接受主键值作为第二个参数。如果我想传递[哈希]值而不是主键,该怎么办?我怎么能用
EntityManager#find
方法做到这一点? 附:我注意到OpenJPA和EclipseLink实现的
EntityManager#find
要求是相同的。嗯...     
已邀请:
我只是在我的一个托管bean中尝试了这个,它正在运行。这是相关代码,它与您的代码基本相同:
@ManagedBean
@RequestScoped
public class TestBean {
    @ManagedProperty(value = "#{param.id}")
    private Long prop;

    @PostConstruct
    public void init() {
        System.out.println(prop);
        // prints 1234 if I go to the url with http://localhost/page.jsf?1234
    }

    public Long getProp() {
        return prop;
    }

    public void setProp(Long prop) {
        this.prop = prop;
    }
}
我在glassfish 3.1.1上运行它。我唯一想到的可能是注入的EJB在某种程度上搞乱了ManagedBean中的请求范围?     

要回复问题请先登录注册