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

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

问题描述

让我们想象一下,我的JSF 2应用程序中有两个页面:第一页面显示一个对象表(汽车或其他东西),另一页面能够显示一个特定对象的详细信息. 该表页面在请求范围内,因为应在每次用户请求时重新加载对象.详细信息页面在视图范围内. 因此,当我单击表内的对象时,该对象应显示在详细信息页面中.通过重定向,我能够转到详细信息页面,但是详细信息页面为空,因为生成了新的vew. 好的,我可以将范围更改为会话,但这会导致其他问题,因此我想在查看范围中使用详细信息页面.有没有一种方法可以将参数传递给新生成的视图范围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

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

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:

Bean of details page:

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只是将汽车的信息加载到已设置到bean中的给定ID中.

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

另请参见:

  • Difference between h:button and h:commandButton
  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?

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

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