Injection in JSF:
The dependent managed bean can be injected in to the managed bean using the @ManagedBean property annotation in JSF2
Say for example TestBean is holding a property of type DepartmentBean, the bean shall be injected to the TestBean as below.
@ManagedBean
@RequestScoped
public class TestBean {
@ManagedProperty(value="5")
private int id;
@NotNull(message="Name cannot be blank")
private String name;
@ManagedProperty(value="#{departmentBean}")
private DepartmentBean deptBean;
public DepartmentBean getDepartmentBean() {
return deptBean;
}
public void setDepartmentBean(DepartmentBean deptBean) {
this.deptBean= deptBean;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
@ManagedBean
@RequestScoped
public class DepartmentBean {
@ManagedProperty(value="admin")
public String dept;
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
<managed-bean>
<managed-bean-name>testBean</managed-bean-name>
<managed-bean-class>com.test.TestBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>deptBean</property-name>
<value>#{departmentBean}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>departmentBean</managed-bean-name>
<managed-bean-class>com.test.DepartmentBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-bean>
The dependent managed bean can be injected in to the managed bean using the @ManagedBean property annotation in JSF2
Say for example TestBean is holding a property of type DepartmentBean, the bean shall be injected to the TestBean as below.
@ManagedBean
@RequestScoped
public class TestBean {
@ManagedProperty(value="5")
private int id;
@NotNull(message="Name cannot be blank")
private String name;
@ManagedProperty(value="#{departmentBean}")
private DepartmentBean deptBean;
public DepartmentBean getDepartmentBean() {
return deptBean;
}
public void setDepartmentBean(DepartmentBean deptBean) {
this.deptBean= deptBean;
}
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
}
@ManagedBean
@RequestScoped
public class DepartmentBean {
@ManagedProperty(value="admin")
public String dept;
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
}
In the above example you could notice the managed bean injection performed using the @ManagedProperty annotation and also EL is been used to refer the dependent bean (#{departmentBean}. You could also notice there is no name attribute is been used in both the managed beans and hence the default value is been assigned with the naming conventions.
The same can be done in JSF1.2 using the faces Config xml configuration
<managed-bean>
<managed-bean-name>testBean</managed-bean-name>
<managed-bean-class>com.test.TestBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-property>
<property-name>deptBean</property-name>
<value>#{departmentBean}</value>
</managed-property>
</managed-bean>
<managed-bean>
<managed-bean-name>departmentBean</managed-bean-name>
<managed-bean-class>com.test.DepartmentBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-bean>
No comments:
Post a Comment