查看不同bean中的参数

发布于 2024-12-01 11:36:14 字数 2160 浏览 0 评论 0原文

我有一个名为 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 个属性 tripIdseats,它们与 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 技术交流群。

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

发布评论

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

评论(2

北方。的韩爷 2024-12-08 11:36:14

f:viewParam 的工作方式与 h:inputText 完全相同。这意味着它使用相同的表达式作为源(渲染时)和目标(更新模型时)。如果你有:

<h:inputText value="#{a.test}" />

你永远不会问“如何使 inputTextb.foo 读取并写入 a.test”,但每个人似乎期望 f:viewParam 出现这种行为。

无论如何,有一个简单的答案。要将任何值附加到链接,您只需使用 f:param

<h:link outcome="/customer/booking.xhtml" value="Book this trip" >
    <f:param name="tripid" value="#{bookingBean.tripId}" />
    <f:param name="seats" value="#{bookingBean.seats}" />
</h:link>

f:viewParam works exactly like h: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:

<h:inputText value="#{a.test}" />

You would never ask "how to make the inputText read from b.foo and write to a.test", yet everyone seems to expect such behavior from f:viewParam.

Anyway, there is a simple answer. To append any values to link, you just need to use f:param:

<h:link outcome="/customer/booking.xhtml" value="Book this trip" >
    <f:param name="tripid" value="#{bookingBean.tripId}" />
    <f:param name="seats" value="#{bookingBean.seats}" />
</h:link>
鹿港小镇 2024-12-08 11:36:14

与以下标记对应的链接内容:

<h:link outcome="/customer/booking.xhtml" value="Book this trip"
            includeViewParams="true" />

是在 trip.xhtml 视图请求的渲染响应阶段生成的。如果您需要 URL 包含视图参数值,则必须在渲染响应阶段完成之前在用于访问视图的 URL 中提供这些值,或者必须在模型中设置这些值。如果您不执行其中任何一项,生成的超链接将包含视图参数的默认值。

简而言之,您必须:

  • 要么确保使用必要的参数访问 trip.xhtml 视图:trip.xhtml?tripid=x&seats=y,特别是如果始终可以使用参数来访问视图。
  • 或者,您必须在 JSF 运行时呈现链接之前执行的方法中设置值。 BookingBean 类的构造函数或类中带注释的方法 @PostConstruct 是设置值的理想位置。如果在视图中执行操作,您还可以在托管 Bean 的其他方法中重置/更新 Bean 的值。请注意,JSF 运行时将调用 getTripIdgetSeats 以将视图参数 tripIdseats 的值包含在生成的 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:

<h:link outcome="/customer/booking.xhtml" value="Book this trip"
            includeViewParams="true" />

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:

  • either ensure that the 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.
  • or you must set the values in a method that gets executed before the link is rendered by the JSF runtime. The constructor of the 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 invoke getTripId and getSeats to include the values of the view parameters tripId and seats 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 for booking.xhtml, instead requiring you to specify the supposedly unexpected value of tripBean is due to the fact that the includeViewParams attribute of the link tag, will include the parameters of the to-view and not the from-view. Simply put, the JSF runtime will invoke bookingBean.getTripId() and bookingBean.getSeats() and not tripBean.getTripId() and tripBean.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 new BookingBean instance is created during this evaluation and the default values are being used instead. You can quite obviously avoid this by using the TripBean instead (and most examples of includeViewParams demonstrate this), or you can set values during the construction of the BookingBean object.

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