Reputation: 2561
Hi i am trying to load list of Students onload of a Page in JSF using h:datatable tag
<h:dataTable value="#{studentBean2.studentList}" var="student">
......
.....
</h:datatable>
now my ManagedBean is as follows
public class StudentBeanTwo {
public StudentBeanTwo() {
init();
}
@Resource(name="jdbc/rahul_sample_pool",type=DataSource.class)
private DataSource dataSource;
private void init(){
.......
.......
if(this.getStudentList() == null){
loadStudents();
}
}
private void loadStudents() throws Exception{
Connection con = null;
.....
.....
try{
if(this.dataSource == null){
System.out.println(" DataSource() is null ");
}
con = this.dataSource.getConnection();
........
}
}
now my Question is why my datasource is null,
i check by annotating @Resource to a variable in another servlet an i am able to create the connection,
so whats the problem in the above managed-bean,
why datasource is null ? The container is not able to inject Resource, why ?
please help me out
Upvotes: 1
Views: 1944
Reputation: 30025
In addition to Björns comment: Injection is done after construction and you call your init method from the constructor.
You could annotate your init()
method with @PostConstruct
. Then it will be called after construction and not during construction.
import javax.annotation.PostConstruct;
...
@PostConstruct
private void init(){
...
if(this.getStudentList() == null){
loadStudents();
}
}
Then the init method gets called every time the bean is constructed (depending on your bean's scope).
Upvotes: 4