简体   繁体   English

在组件绑定上绑定 f:viewParam

[英]Binding f:viewParam on component binding

I want to create <f:viewParam> which sets active tab of <p:tabView> .我想创建<f:viewParam>来设置<p:tabView>活动选项卡。 In my page I define a <f:viewParam> :在我的页面中,我定义了一个<f:viewParam>

<f:viewParam name="tab" value="#{tabView.activeIndex}" />

and <p:tabView> with binding :<p:tabView> binding

<p:tabView binding="#{tabView}">
    <p:tab title="tab1" />
    <p:tab title="tab2" />
    <p:tab title="tab3" />
</p:tabView>

But it's not working.但它不起作用。 If I open my page with parameter ?tab=1 I see this exception:如果我用参数?tab=1打开我的页面,我会看到这个异常:

Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'tabView' resolved to null引起:javax.el.PropertyNotFoundException:目标无法访问,标识符“tabView”解析为空

Why is this happening?为什么会这样?

PS: in addition to @BalusC answer, you can use binding component property on the viewMap like this: PS:除了@BalusC 的回答,你还可以像这样在 viewMap 上使用绑定组件属性:

<f:viewParam name="tab" value="#{viewScope['activeTabIndex']}" />

<p:tabView activeIndex="#{viewScope['activeTabIndex']}">
    <p:ajax event="tabChange" />
    <p:tab title="tab1" />
    <p:tab title="tab2" />
    <p:tab title="tab3" />
</p:tabView>

in this case you must provoke ajax-request to put value in the viewMap through <p:ajax event="tabChange" /> or dynamic="true" cache="false"在这种情况下,您必须通过<p:ajax event="tabChange" />dynamic="true" cache="false"激发 ajax-request 将值放入 viewMap

That's because the view is not yet built at that moment and thus all binding attributes are not yet evaluated.那是因为此时视图尚未构建,因此尚未评估所有binding属性。 This is indeed somewhat unexpected, but on a GET request the view is by default only built during render response phase.这确实有点出乎意料,但在 GET 请求中,视图默认仅在渲染响应阶段构建。 It works fine when the request is a postback as the view is by definition built during restore view phase already.当请求是回发时,它工作正常,因为根据定义,视图已经在恢复视图阶段构建。

I can think of two workarounds:我可以想到两种解决方法:

  1. Move the <f:viewParam value> into <f:event type="preRenderView" listener> .<f:viewParam value><f:event type="preRenderView" listener> On a GET request, the view is at that point guaranteed built.在 GET 请求中,视图在那个点被保证构建。

     <f:metadata> <f:viewParam name="tab" /> <f:event type="preRenderView" listener="#{tabView.setActiveIndex(tab)}"/> </f:metadata>

    This works, because <f:viewParam> without a value will by default put the converted and validated request parameter value as a request attribute under the very same name #{tab} .这是有效的,因为没有value <f:viewParam>默认将转换和验证的请求参数值作为请求属性放在相同的名称#{tab} You only need to make sure that you don't have request scoped managed beans on the same name, or it may clash.您只需要确保您没有使用相同名称的请求范围的托管 bean,否则可能会发生冲突。

  2. If you don't need conversion and validation, then just pass #{param.tab} directly.如果不需要转换和验证,那么直接传递#{param.tab}即可。

     <f:metadata> <f:event type="preRenderView" listener="#{tabView.setActiveIndex(param.tab)}"/> </f:metadata>

Nice approach, by the way.顺便说一句,不错的方法。

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM