如何在 JSF2/JPA/无状态 EBJ(jsf 托管控制器)中从一个视图导航到下一个视图并保留数据

发布于 2024-12-20 10:20:16 字数 1844 浏览 2 评论 0原文

我有一个使用 JSF2 和 JPA 实体的 Web 应用程序,无状态 ejb 会话 bean 作为我的外观/ejb 对象,托管 bean(请求和视图范围)作为公开业务方法的控制器,托管控制器从注入的无状态会话 bean 中提取数据。

但我很困惑如何在此环境中的控制器中跨视图导航和保留数据,例如:

我有一个 jsf2 视图页面 (departmentView.xhtml),它显示 Department 对象列表,并且每行都有一个编辑项。单击编辑我想加载新页面并显示 在新页面上该部门的列表或员工,所以我调用员工控制器,将所选部门传递给它,

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 环境中实现此功能的正确方法吗?

谢谢

I have a web app using JSF2 with JPA Entities, Stateless ejb session beans as my facade/ejb objects, and managed beans (request and view scoped) as controllers exposing business methods, managed controllers are pulling data from the injected stateless session beans.

but i am confused how to navigate and retain data accross views in my controllers in this environment, for example:

I have a jsf2 view page (departmentView.xhtml) that displays a list of Department objects and each row has an edit item. Clicking edit I want to load a new page and display
a list or Employee's for that department on the new page, so i invoke the Employee controller passing it the selected Department

action="#{employeeController.getEmployeeListForADepartment(ithDepartment)}" 

here is a snippet of my 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>

and my employeeController is defined as

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";
}

i really want to avoid using jsf session scope, and believe there is a way to do this, just not reading about it in any of my jsf/ejb books.

thinking outloud, maybe don't have EmployeeController.getEmployeeListForADepartment(..) do a lookup, just create a parameter from the department id and pass that along via return "employeeView?departmentId=X"; and have constructor then do a lookup if the id is present?

can somehelp me with the proper way to implement this in EJB/JSF2 environment

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-12-27 10:20:16

在源视图中使用简单的 GET 来传递部门 ID,并在目标视图中使用 来转换和设置部门。

例如

<h:link id="editId" value="Edit" outcome="employeeView">
    <f:param name="departmentId" value="#{ithDepartment.id}" />
</h:link>

<f:metadata>
    <f:viewParam name="id" value="#{editDepartmentBean.department}"
        converter="#{departmentConverter}" converterMessage="Bad request. Unknown department."
        required="true" requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>

另请

@ManagedBean
@ViewScoped
public class EditDepartmentBean {

    private Department department;

    // ...
}

参阅:

Use a simple GET <h:link> in the source view to pass the department ID and a <f:viewParam> in the target view to convert and set the department.

E.g.

<h:link id="editId" value="Edit" outcome="employeeView">
    <f:param name="departmentId" value="#{ithDepartment.id}" />
</h:link>

with

<f:metadata>
    <f:viewParam name="id" value="#{editDepartmentBean.department}"
        converter="#{departmentConverter}" converterMessage="Bad request. Unknown department."
        required="true" requiredMessage="Bad request. Please use a link from within the system." />
</f:metadata>

and

@ManagedBean
@ViewScoped
public class EditDepartmentBean {

    private Department department;

    // ...
}

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文