@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
要求是相同的。嗯...
没有找到相关结果
已邀请:
1 个回复
遣莫捅炭
我在glassfish 3.1.1上运行它。我唯一想到的可能是注入的EJB在某种程度上搞乱了ManagedBean中的请求范围?