@ManagedProperty annotated type returns null
I have this Service bean:
@Stateless
public class BookService
{
@PersistenceContext(unitName="persistentUnit")
protected EntityManager entityManager;
public BookModel find(Long id) {
return entityManager.find(BookModel.class, id);
}
}
And the backing bean for the Facelet page is:
@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;
}
}
Why is the value of id
always returns null even though the URL returned as bookedit.jsf?id=5418
I don't understand this.
Also, I find the EntityManager#find
method quite restrictive in that it only accept a primary key value as the second parameter.开发者_运维知识库 What if I want to pass a [hashed] value instead of the primary key. How can I do this with the EntityManager#find
method?
P.S. I notice the EntityManager#find
requirement is the same for both OpenJPA and EclipseLink implementations. Hmm...
I just tried this in one of my managed beans, and it is working. Here's the relevant code, it's basically the same as yours:
@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;
}
}
I'm running this on glassfish 3.1.1. The only thought I had is maybe the injected EJB is somehow messing up the request scope in the ManagedBean?
精彩评论