我有一个使用带有 JPA 实体的 JSF2 的 Web 应用程序,无状态 ejb 会话 bean 作为我的外观/ejb 对象,托管 bean(请求和视图范围)作为公开业务方法的控制器,托管控制器从注入的无状态会话 bean 中提取数据。
但我很困惑如何在这种环境下在我的控制器中导航和保留视图中的数据,例如:
我有一个显示部门对象列表的 jsf2 视图页面 (departmentView.xhtml),并且每一行都有一个编辑项。单击编辑我想加载一个新页面并在新页面上显示该部门的列表或员工,因此我调用员工控制器将其传递给选定的部门
action="#{employeeController.getEmployeeListForADepartment(ithDepartment)}"
这是我的 departmentView.xhtml 的片段
<h:dataTable id="table" value="#{departmentController.departmentList}"
var="ithDepartment">
...
<h:column>
<h:commandLink id="editId" value="Edit"
action="#{employeeController.getEmployeeListForADepartment(ithDepartment)}" />
</h:column>
我的employeeController被定义为
ManagedBean(name = "employeeController")
@ViewScoped
public class EmployeeController implements Serializable {
...
private List<Employee> employeeList = new ArrayList<Employee>();
...
@EJB
private com.ejb.session.EmployeeFacade ejbEmployeeFacade;
...
public List<Employee> getEmployeeListForADepartment(Department dept)
{
if(employeeList==null || employeeList.isEmpty())
employeeList = ejbEmployeeFacade.findEmployeesByDepartment(dept);
// now i want to navigate to the employee view showing these employees for the
// selected department.
// but this navigation below triggers creating a new EmployeeController
// and i lose my employeeList
return "employeeView";
}
我真的想避免使用 jsf 会话范围,并相信有办法做到这一点,只是不在我的任何 jsf/ejb 书籍中阅读它。
大声思考,也许不需要EmployeeController.getEmployeeListForADepartment(..)
查找,只需从部门 id 创建一个参数并将其传递给通过return "employeeView?departmentId=X";
构造函数,然后在 id 存在时进行查找?
可以帮助我在 EJB/JSF2 环境中以正确的方式实现它
谢谢