查看不同bean中的参数
我有一个名为 trip.xhtml
的页面,我使用以下代码从 URL 中获取参数:
<f:metadata>
<f:viewParam name="tripid" value="#{tripBean.tripId}" />
<f:viewParam name="seats" value="#{tripBean.seats}" />
<f:event type="preRenderView" listener="#{tripBean.processParams}" />
</f:metadata>
TripBean
看起来像这样(简化的)
@ManagedBean
@RequestScoped
public class TripBean implements Serializable {
private static final long serialVersionUID = -4885781058258859229L;
private Long tripId;
private int seats;
// Getters and setters for tripId and seats
: trip.xhtml
页面我有一个 h:link
:
<h:link outcome="/customer/booking.xhtml" value="Book this trip"
includeViewParams="true" />
我期望的是,当我单击此链接时得到的 URL 类似于“/customer/booking.jsf?tripid=2&seats=1”
。仅当我将以下代码放在 booking.xhtml
页面上时才会出现这种情况:
<f:metadata>
<f:viewParam name="tripid" value="#{tripBean.tripId}" />
<f:viewParam name="seats" value="#{tripBean.seats}" />
</f:metadata>
尽管我真正想要的是使用另一个 bean。将代码更改为:
<f:metadata>
<f:viewParam name="tripid" value="#{bookingBean.tripId}" />
<f:viewParam name="seats" value="#{bookingBean.seats}" />
</f:metadata>
BookingBean
还有 2 个属性 tripId
和 seats
,它们与 TripBean
相同,但是当我现在尝试单击该链接时,我只看到一个设置为 0 的席位参数。 ("/customer/booking.jsf?seats=0"
)
有谁知道为什么我似乎无法通过当我尝试使用另一个 bean 来存储它们时,是否将 viewparams 传递到另一个页面?如果无法将其存储在另一个 bean 中,我如何将 TripBean
中的这些值放入 BookingBean
中?
我使用的快速解决方法: 不使用 includeViewParams="true"
,而是手动向链接添加参数(见下文)“修复”问题。尽管我仍然想知道为什么它不能与 includeViewParams
一起使用!
<h:link outcome="/customer/booking.xhtml" value="#{msg['page.trip.booking']}">
<f:param name="tripid" value="#{tripBean.tripId}" />
<f:param name="seats" value="#{tripBean.seats}" />
</h:link>
I've got a page called trip.xhtml
where I take parameters out of the URL using the following code:
<f:metadata>
<f:viewParam name="tripid" value="#{tripBean.tripId}" />
<f:viewParam name="seats" value="#{tripBean.seats}" />
<f:event type="preRenderView" listener="#{tripBean.processParams}" />
</f:metadata>
The TripBean
looks like this (simplified):
@ManagedBean
@RequestScoped
public class TripBean implements Serializable {
private static final long serialVersionUID = -4885781058258859229L;
private Long tripId;
private int seats;
// Getters and setters for tripId and seats
On the bottom of the trip.xhtml
page I have a h:link
:
<h:link outcome="/customer/booking.xhtml" value="Book this trip"
includeViewParams="true" />
What I expect is that the URL I get when I click this link is something like "/customer/booking.jsf?tripid=2&seats=1"
. This is only the case when I put the following code on my booking.xhtml
page:
<f:metadata>
<f:viewParam name="tripid" value="#{tripBean.tripId}" />
<f:viewParam name="seats" value="#{tripBean.seats}" />
</f:metadata>
Although what I actually want is to use another bean. Changing the code to:
<f:metadata>
<f:viewParam name="tripid" value="#{bookingBean.tripId}" />
<f:viewParam name="seats" value="#{bookingBean.seats}" />
</f:metadata>
The BookingBean
also has 2 properties tripId
and seats
which are identical to the TripBean
, but when I try to click the link now, I only see a seats-parameter which is set to 0. ("/customer/booking.jsf?seats=0"
)
Does anyone have any idea why I can't seem to pass the viewparams to the other page when I'm trying to use another bean to store them in? And IF it is impossible to store it in another bean, how can I put those values from TripBean
in BookingBean
?
Quick work-around I used:
Not using includeViewParams="true"
, but adding parameters to the link manually (see below) "fixes" the problem. Although I'm still wondering why it won't work with includeViewParams
!
<h:link outcome="/customer/booking.xhtml" value="#{msg['page.trip.booking']}">
<f:param name="tripid" value="#{tripBean.tripId}" />
<f:param name="seats" value="#{tripBean.seats}" />
</h:link>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
f:viewParam
的工作方式与h:inputText
完全相同。这意味着它使用相同的表达式作为源(渲染时)和目标(更新模型时)。如果你有:你永远不会问“如何使
inputText
从b.foo
读取并写入a.test
”,但每个人似乎期望 f:viewParam 出现这种行为。无论如何,有一个简单的答案。要将任何值附加到链接,您只需使用
f:param
:f:viewParam
works exactly likeh:inputText
. This means that it uses the same expression as a source (when rendering) and as a target (when updating the model). If you had:You would never ask "how to make the
inputText
read fromb.foo
and write toa.test
", yet everyone seems to expect such behavior fromf:viewParam
.Anyway, there is a simple answer. To append any values to link, you just need to use
f:param
:与以下标记对应的链接内容:
是在
trip.xhtml
视图请求的渲染响应阶段生成的。如果您需要 URL 包含视图参数值,则必须在渲染响应阶段完成之前在用于访问视图的 URL 中提供这些值,或者必须在模型中设置这些值。如果您不执行其中任何一项,生成的超链接将包含视图参数的默认值。简而言之,您必须:
trip.xhtml
视图:trip.xhtml?tripid=x&seats=y
,特别是如果始终可以使用参数来访问视图。getTripId
和getSeats
以将视图参数tripId
和seats
的值包含在生成的 URL,因此您还应该验证这些方法的行为。您的问题是,为什么您无法在
booking.xhtml
的 EL 表达式中指定bookingBean
,而是要求您指定tripBean
的预期意外值> 是因为link
标记的includeViewParams
属性将包含to-view
的参数,而不是>从视图
。简而言之,JSF 运行时将调用bookingBean.getTripId()
和bookingBean.getSeats()
,而不是tripBean.getTripId()
和tripBean.getSeats()
。虽然这看起来可能违反直觉,但您应该了解 JSF 规范对这种情况的处理方式截然不同。在用户执行的大多数交互中,组件的操作 URL 是在用户执行操作时生成的,直到用户执行该操作时才生成。另一方面,
link
标记需要抢先计算 URL,因此 JSF 运行时将其视为“抢先导航”,这与此完全不同。构造 URL 时,与目标视图的视图参数相关的对象必须可访问,以使 URL 有意义。 URL 中的值为 0 的原因是,在此评估期间创建了一个新的BookingBean
实例,并且改为使用默认值。您可以通过使用 TripBean 来避免这种情况(大多数 includeViewParams 示例都证明了这一点),或者您可以在构建 BookingBean 对象期间设置值。The link content corresponding to the following tag:
is generated during the render-response phase of the request for the
trip.xhtml
view. If you need the URL to contain the view parameter values, then you must provide these values in the URL used to access the view, or you must set these values in the model, before the render-response phase is complete. If you do not perform either of these, the generated hyperlink will contain the default values for the view parameters.In simpler words, you must:
trip.xhtml
view is accessed with the necessary parameters:trip.xhtml?tripid=x&seats=y
, especially if the view is to be accessed with parameters at all times.BookingBean
class or a@PostConstruct
annotated method in the class would be ideal places to set the values. You could also reset/update the values of the bean in other methods of your managed bean, in the event of actions being performed in the view. Note, that JSF runtime will invokegetTripId
andgetSeats
to include the values of the view parameterstripId
andseats
in the resulting URL, so you ought to verify the behavior of these methods as well.Your question on why you are unable to specify
bookingBean
in the EL expression forbooking.xhtml
, instead requiring you to specify the supposedly unexpected value oftripBean
is due to the fact that theincludeViewParams
attribute of thelink
tag, will include the parameters of theto-view
and not thefrom-view
. Simply put, the JSF runtime will invokebookingBean.getTripId()
andbookingBean.getSeats()
and nottripBean.getTripId()
andtripBean.getSeats()
.While this might seem counter-intuitive, you ought to understand that the JSF specification treats this scenario quite differently. In most interactions performed by a user, the action URL of a component is generated when the user performs the action and not until then. The
link
tag on the other hand, requires preemptive computation of the URL, and hence the JSF runtime treats this quite differently as "pre-emptive navigation". When the URL is being constructed, the objects pertaining to the view parameters of the target view must be accessible, in order for the URL to be meaningful. The reason for the values being 0 in the URL, is that a newBookingBean
instance is created during this evaluation and the default values are being used instead. You can quite obviously avoid this by using theTripBean
instead (and most examples of includeViewParams demonstrate this), or you can set values during the construction of theBookingBean
object.