且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将参数传递给JSF中的视图作用域bean

更新时间:2023-10-06 13:13:01

使用视图参数是解决您情况的最正确方法.您实际上不需要执行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?