将参数传递给 JSF 中的视图作用域 bean [英] Passing parameters to a view scoped bean in JSF

查看:28
本文介绍了将参数传递给 JSF 中的视图作用域 bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的 JSF 2 应用程序中有两个页面:第一个页面显示一个对象表(汽车或其他),另一个页面能够显示一个特定对象的详细信息.表格页面在请求范围内,因为每次用户请求时都应该重新加载对象.详细信息页面在视图范围内.所以当我点击表格内的一个对象时,这个对象应该显示在详细信息页面中.通过重定向,我可以转到详细信息页面,但详细信息页面是空的,因为生成了一个新视图.好的,我可以将范围更改为会话,但这会导致其他问题,因此我希望将详细信息页面置于视图范围内.有没有办法将参数传递给新生成的视图范围 bean?

Lets imagine I have two pages in my JSF 2 application: the first page displays a table of objects (cars or whatever) and another page is able to display the details of one specific object. The table page is in request scope because the objects should be reloaded each time the user requests it. The detail page is in view scope. So when I click on an object inside the table, this object should be displayed in the details page. With a redirect I am able to go to the details page but the detail page is empty, because a new vew was generated. Ok, I could change the scope to session but this would lead to other problems, so I would like to have the details page in view scope. Is there a way of passing an argument to a newly generted view scope bean?

更新 1

这是第一次尝试使用视图参数的代码片段.正如评论的那样,这是行不通的.该值作为请求参数传递,但在目标页面中该值为空.

Here is the code snippet with the first try of using view parameters. As commented this does not work. The value gets passed as request parameter but in the target page the value is null.

表格页面:

<h:button value="Go to details" outcome="targetPage">
     <f:param name="carId" value="This is a test" />
</h:button>

详情页面:

<f:metadata>
    <f:viewParam name="carId" value="#{bean.id}" />
    <f:event type="preRenderView" listener="#{bean.loadData}"/>
</f:metadata>

详情页的Bean:

private String id;

public String getId() {
    return id;
}

public void setId( String id ) {
    this.id = id;
}

public void loadData() {
    System.out.println( "Id: " + id );
}

更新 2

我发现了我的错误.元数据部分位于模板文件中.当我将标签放入详细信息页面的主文件时,它可以工作.非常感谢.

I have found my mistake. The metadata part was inside a template file. When I put the tag to the main file for the details page it works. Thank you very much.

推荐答案

使用视图参数是最适合您的情况的方法.您实际上不需要执行 REDIRECT,而是执行简单的 GET 请求:

Using view parameters is the most proper way for your case. You don't really need to perform a REDIRECT, but a plain GET request:

<h:button value="Go to details" outcome="carDetails">
    <f:param name="carId" value="#{currentCar.id}" />
</h:button>

这会将您指向以下地址:carDetails.xhtml?carId=1

This will point you to this address: carDetails.xhtml?carId=1

之后,在您的 carDetails.xhtml 页面中,获取视图参数并加载该汽车的信息:

After that, in your carDetails.xhtml page, grab the view param and load the info for that car:

<f:metadata>
    <f:viewParam name="carId" value="#{carDetailBean.carId}" />
    <f:event type="preRenderView" listener="#{carDetailBean.loadData}"/>
</f:metadata>

其中 CarDetailBean#loadData 只是加载您的汽车的信息以显示给定的 id,已经设置到 bean 中.

Where CarDetailBean#loadData just loads the info for your car to display with the given id, already set into the bean.

另见:

这篇关于将参数传递给 JSF 中的视图作用域 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆