1

I'm writing a simple menu using PrimeFaces, and I'm wondering if I can access a tag attribute within itself. Let me explain:

...
<p:menuitem value="#{labels['menu.home']}" url="/pages/index.jsf"
            styleClass="#{view.viewId == '/pages/index.jsf' ? 'nav-selected-menuitem' : ''}"/>
...

Since this will be repeated for more pages, is there a way to do something of the sort:

<p:menuitem value="#{labels['menu.home']}" url="/pages/index.jsf"
            styleClass="#{view.viewId == url ? 'nav-selected-menuitem' : ''}"/>

Where url is the same url attribute in this p:menuitem tag.

Is this doable?

rion18
  • 1,141
  • 16
  • 20

1 Answers1

1

The component itself is available by #{component}.

So, this should do:

<p:menuitem ... url="/pages/index.jsf"
    styleClass="#{view.viewId == component.url ? 'nav-selected-menuitem' : ''}" />

Noted should be that this may fail if the component's renderer is badly implemented (i.e. it doesn't properly do pushComponentToEL() as mandated by encodeBegin()). This is in turn worth an issue report at component library maintainer.

See also:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • For some reason, calling `component` references `p:menuitem`'s parent, a `p:menu` component. I guess I could navigate to the `p:menuitem` component from there... – rion18 Mar 17 '16 at 15:37
  • I'd say its renderer is badly implemented. This is not the first time I see it on a PrimeFaces component. Report an issue to make them aware and get them to fix it. – BalusC Mar 17 '16 at 15:38
  • Thanks so much BalusC. As usual, you are the JSF god. – rion18 Mar 17 '16 at 15:39