5

I'm writing an application in Java EE 6 and using Primefaces 3.4.1 for the user interface.

I have something like that.

genericPage.xhtml

<ui:composition template="mainApplicationTemplate.xhtml" [...]>
    <p:tabView id="tabView" dynamic="false"cache="false">
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form1">
                ...
            </h:form>
        </p:tab>
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form2">
                ...
            </h:form>
        </p:tab>
    </p:tabView>
<ui:composition >

child1.xmtml

<ui:composition template="genericPage.xhtml" ...>
    <ui:param name="actionBean" value="#{actionBeanA}"/>
</ui:compisition>

child2.xmtml

<ui:composition template="genericPage.xhtml" ...>
    <ui:param name="actionBean" value="#{actionBeanB}"/>
</ui:compisition>

The idea behind that is that child1.xhtml and child2.xhtml share the same jsf code, all contained in the genericPage.xhtml, but they have different backend bean (parametrised by "actionBean")

So far that works really well. It gets complicated when I put the ui parameter inside an <p:ajax/> element.

From the backend bean, I need to programmatically update the active tab, leaving the other untouched. In order to that, I need to store the active tab in the action bean, When certain external events occur, the action bean updates the active tabs.

Note that due to some other factors:

  • I cannot set dynamic="true" in the tabView
  • I cannot have a global form around the tabView and thus cannot use the 'activeIndex' property (which I am doing in an other part of the application) to manage the active tab.

What I want to do

To solve this issue, I want to use the tabChange event of the tabView element:

<p:tabView id="tabView" dynamic="false"cache="false">
        <p:ajax event="tabChange" listener="#{actionBean.listen}"
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form1">
                ...
            </h:form>
        </p:tab>
        <p:tab id="tab1" title="Tab 1">
            <h:form id="form2">
                ...
            </h:form>
        </p:tab>
    </p:tabView>

action bean

@Named
@WindowScoped
public class ActionBeanA implements Serializable{
    public void listen(TabChangeEvent event){
        ...
    }

}

What doesn't work

When I do that, I'm getting the error

Target Unreachable, identifier 'actionBean' resolved to null: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'actionBean' resolved to null

This seems to indicate that the <p:ajax> element has not been passed the action bean and therefore doesn't know what actionBean is.

However if I change the signature of the listener method like that

<p:ajax event="tabChange" listener="#{actionBean.listen(anything)}"

and change the backend bean to something like:

public void listen(TabChangeEvent event){
    System.out.println(event.toString());
}

Doing that, I am not getting the target unreachable error but a null pointer exception in the listen method (because I haven't assigned a value to "anything"). That shows that in this case, the <p:ajax/> elements knows what actionBean is and manages to call the method in the bean.

Questions

How could I get around this problem? I'd like to be able, on a tab change event, to send to my backend bean the new active tab.

phoenix7360
  • 2,807
  • 6
  • 30
  • 41
  • 1
    You don't need to pass anything to the `void listen(TabChangeEvent event)`. Just keep this signature on the method and use `listener="#{actionBean.listen}"`, the framework takes care of passing the event for you. – Elias Dorneles Jan 22 '13 at 14:29
  • 1
    As I explained, I get the "Target Unreachable" when I do `listener="#{actionBean.listen}"`. – phoenix7360 Jan 22 '13 at 16:51
  • right, but you also wrote about changing the signature of the listener method, so I was just pointing out that the correct use is you do not pass anything. But I hear you, you want to say that when you pass an argument in the EL, it suddenly seems to find the bean it complains when you don't pass anything. Only wanted to say that this test should not require any changes in the backing bean code. – Elias Dorneles Jan 22 '13 at 18:05
  • OK I see what you mean. However, I actually never changed the signature on the backend method, which always has been `void listen(TabChangeEvent event)`. I was talking about changing the signature of the call in the front end: `listener="#{actionBean.listen}"` vs `listener="#{actionBean.listen(something)}"`. I realise that I shouldn't try to pass the event myself but because there seems to be a bug I was just trying to find my way around it. – phoenix7360 Jan 22 '13 at 18:21
  • So what is the result of invoking it without parameter? – Aritz Jan 22 '13 at 23:36
  • It seems you're doing all this stuff to control which tab is selected in order to update it, why just not update the form you're into? That should maintain you on your selected tab. – Aritz Jan 22 '13 at 23:43
  • The problem is that I don't have one global form but one per tab. When some external event happens, the backend been needs to run a specific method (depending on which tab is selected) and update the corresponding form. The reason behind is that the backend methods for each tab are expensive. Thus I only want to update the active one. – phoenix7360 Jan 23 '13 at 09:03

2 Answers2

6

I was also having this problem and found the solution in Primefaces issue tracker: Solution found in the post #20

To summarise, this is a known problem in Primefaces. The semi-fix is found in the release 3.4.2 and requires some changes to the code. At the moment you have:

public void listen(TabChangeEvent event){
    System.out.println(event.toString());
}

Which does not work. You should change your code to:

public void listen(AjaxBehaviorEvent event){
    System.out.println(event.toString());
}

And if you need to use specific methods of TabChangeEvent then you need to do the casting: (TabChangeEvent)event.

It has a status fixed on the issue tracker so it may require this as a permanent solution.

bjedrzejewski
  • 2,378
  • 2
  • 25
  • 46
-1

There's a difference between value and method expression processing. For the latter, you need to have the parameter available a bit later - in the Facelet Context.

To include the value of the parameter into the facelet context, you simply have to put the parameter value definition inside of an <f:metadata> tag.

Also, note that the <f:metadata> tag should always be a direct child of the <f:view> tag, so I would do something like this:

template.xhtml

<f:view ....>
    <ui:insert name="metadata"/>
    ....
</f:view>

childPage.xhtml

<ui:composition template="/WEB-INF/template.xhtml">
    <ui:define name="metadata">
        <f:metadata>
            <ui:param name="actionBean" value="#{actionBeanA}"
        </f:metadata>
    </ui:define>
</ui:composition>

After doing this, you may use the listener attribute in the template.xhtml the original way with no workarounds and your method will always be called with the appropriate event type parameter.

Feva
  • 59
  • 3